Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do i save the inputs of program into a file from the program.
Python only:
i need to add it to this:
TASK 2 AGAIN
sentence = input("Please input a sentence: ")
print(sentence)
word=input("Please enter word and position wil be shown: ")
if word in sentence:
print("Found word")
else:
print ("Word not found")
But i haven't got a clue
I assume this is what you're asking for
text_file = open("Output.txt", "w")
text_file.write(stuff)
text_file.close()
Your question seems to have two main parts to it: how do I get inputs in Python and how do I save data to a file in Python.
To get input from the terminal:
>>> data = input('Input: ')
>>> Input: hello, world!
To save to a file:
>>> with open('output.txt', 'w') as f:
>>> f.write(data)
You can find more information about inputs here and file i/o here
Edit 0: #Gazzer, if you want to save sentence + input you'll need to do f.write(sentence + input) rather than using .save().
Edit 1: #Gazzer, I got something like the below to work (note: the code does not show position of the found word):
sentence = input("Please input a sentence: ")
word = input("Please enter word and position wil be shown: ")
with open('output.txt', 'w') as f:
f.write('{} {}'.format(sentence, word))
If you run into more issues, there are hundreds of resources all over the web to ask for help. Stack Overflow, learnprogramming, and many more.
Next time you ask a question, it is really helpful for those answering if you provide a code snippet you are working on and what the problem/errors are.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying a simple code but I'm stuck. I want to ask a question to the user and store the answer to an input, and then write it on a file.
Here's the code :
input = open("Wishes.txt", "w")
wish = input("What do you wish ?")
input.write(wish)
print("Thank you")
I get this error : TypeError: '_io.TextIOWrapper' object is not callable
I'm sure it is really easy but I'm a beginner so I don't know where to find a solution. Thank you in advance.
You have hidden the function name input by creating a variable called input. Simply rename the variable.
Also you probably want to append to the file instead of overwriting the whole thing. And you should be using with to properly handle an external resource.
wish = input("What do you wish ?")
with open("Wishes.txt", "a") as file:
file.write(wish + "\n")
print("Thank you")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a task i need some help with. The task is to create a python script that asks the user to enter a desired username. The username has to be as following: "a11aaaaa".
So starting with a letter, 2x numbers, 5x letters. This is the rule for how the username should look and if the given input does not match that, the user shall be able to try again until getting it right.
Thankful for any help!
As I recently learnt from another user on SO, \w includes \d. Therefore, '^\w\d{2}\w{5}$', as suggested by some users here, will match, for example, 12345678.
To fix that, just specify the character class explicitly:
import re
regex = re.compile('^[A-Za-z]\d{2}[A-Za-z]{5}$')
while True:
password = input('Please enter a password: ')
if regex.search(password):
print('Yay! Your password is valid!')
break
else:
print("Oh no, that's not right. You need a letter, then two numbers, then five letters. ", end='')
^\w{2}\d{2}\w{5}$
You can use this handy site to experiment with regex: https://regex101.com/
You can do something like that:
import re
while True:
name = input('Enter your name')
if re.match('^\w\d{2}\w{5}$', name):
break
Try this one:
import re
uname = input("Enter your username: ")
regex = re.compile(r"^[A-Za-z]{1}\d{2}[A-Za-z]{5}$")
if regex.findall(uname):
print ("Valid username")
else:
print ("Invalid username")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I know this is probably a simple problem but as a beginner I thought I would ask. I have created a simple Python script to run through terminal where, after being asked three questions, the user will be given an output with it all in. I would like to add to this by capitalising all words answered and I know I could use the .title function but im not sure where to put it. any help would be much appreciated.
#ask user for age
name = input('What is your name?: ')
print(name)
#ask user age
age = input('How old are you?: ')
print(age)
#ask user for city
city = input('What city were you born in?: ')
print(city)
#ask user what they enjoy
hobby = input('What do you enjoy doing in your spare time?: ')
print(hobby)
#create output text
string = 'Your name is {} and you are {} years old. you were born in {} and you enjoy {}'
output = string.format(name, age, city, hobby)
#print output to screen
print(output)
You will put it in this line:
output = string.format(name.title(), age.title(), city.title(), hobby.title())
This is pretty straight forward since this is what you add to the final output.
You could also add it straight after asking for input like this:
my_name = input('Enter your name:').title()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi there i am trying to put a user input into an array, so far i have:
#Decrypt string
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
#Put input into array
#????
I am tring to get the input and put it in an array so that it can be referenced using
chr(text[1])
To convert it into plain text from ascii (Basic encryption and decryption).
I have found a few posts on this but they are outdated (for python2 etc...).
Thanks!
If you just want to have an indexable list to store user inputs as they come in, you can use the built-in list class and its append method:
keys = list();
texts = list();
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
texts.append(text)
keys.append(key)
Now, texts[n] will return the nth text value entered by your user.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that
Takes a text file with several sentences
Asks the user if they want to compress/decompress the file.
If Compress is chosen, the sentence will have all the unique words and the positions of these words needed to recreate the sentence again.
If decompress is chosen, the compressed text file will need to be found and using the position list given and the unique words - decompressed - the several sentences in the files need to be on separate lines.
Here is the code I have managed to create. It is a subroutine and it's rather faulty.
uniqueWords = []
positions = []
file =
def valChoice():
choice = (" ")
while choice not in ["compress", "decompress"]:
choice = input("Choose compress or decompress").lower()
if choice not in ["compress", "decompress"]:
print("Please input compress or decompress")
finalChoice = valChoice()
if finalChoice = ("compress"):
print("This where i get confused..")
elif finalChoice = ("decompress"):
print("This where i get confused..")
What is wrong with this code? How can I fix it?
With my caveat above, I'll take a shot at what I think you're asking.
To compress the file, iterate through the input words. Store each word reference in a dictionary: the word itself is the key, and its position is the value. If the word is already in the dictionary, then add the new position reference to the existing list of references.
Decompression works in reverse: make a sequence of positions and words. Sort that sequence into ascending order. Concatenate the words to make the original text.
Is that the level of help you need right now?