Reputation: 3
I'm fairly new to Python, I'm learning it at school and I've been messing around with it at home, I'd like to learn it better for when GCSE's hit just to make it easier.
I'm having an issue with the code below:
def takeinfo():
print("To begin with, would you like to write your file clean? If you're making a new file, yes.")
choice=input()
if 'Y' or 'yes' or 'Yes' or 'YES' in choice:
print("What would you like to write in the file? \n")
information=input()
writeinfo()
else:
exit()
def writeinfo():
a=open('names.txt','wt')
a.write(information)
a.close()
takeinfo()
When I type 'Yes' to be taken to the writeinfo() definition, it doesn't write the information I'm asking it to because it's unassigned, even after typing something in the takeinfo() definition? Any help would be appreciated. I understand this is simple, but I've looked at other questions and I can't seem to find what's wrong with my code.
Thankyou.
Upvotes: 0
Views: 44
Reputation: 31284
other answers show good functional style (pass information
to the writeinfo()
function)
for completeness' sake, you could also use global variables.
the problem you are facing is, that the line
information=input()
(in takeinfo()
) will assign the input to a local variable, that shadows the global variables of the same name.
in order to force it to the global variable, you have to explicitely mark it as such, using the global
keyword.
input
vs raw_input
input()
in python3 will just ask you to input some data.
however, in Python2 it will evaluate the user-data. on py2 you should therefore use raw_input()
instead.
there's another problem with your evaluation of choice
(whether to write to file or not).
the term 'Y' or 'yes' or 'Yes' or 'YES' in choice
always evaluates to true, since it is really interpreted as ('Y') or ('yes') or ('Yes') or ('YES' in choice)
, and bool('Y')
is True
.
instead, you should use choice in ['Y', 'yes', 'Yes', 'YES']
to check whether choice
is one of the items in the list.
you can further simplify this by normalizing the user answer (e.g. lower-casing it, removing trailing whitespace).
try:
input = raw_input
except NameError:
# py3 doesn't know raw_input
pass
# global variable (ick!) with the data
information=""
def writeinfo():
a=open('names.txt','wt')
a.write(information)
a.close()
def takeinfo():
print("To begin with, would you like to write your file clean? If you're making a new file, yes.")
choice=input()
if choice.lower().strip() in ['y', 'yes']:
print("What would you like to write in the file? \n")
global information
information=input()
writeinfo()
else:
exit()
Upvotes: 0
Reputation: 33724
You will need to pass the argument on like this:
def takeinfo():
# same as before
information=input()
writeinfo(information)
# else stays the same
def writeinfo(information):
# rest remains the same...
takeinfo()
Or just change information into the global scope using global
.
And a hint for you:
if 'Y' or 'yes' or 'Yes' or 'YES' in choice:
Wouldn't work as you would've expected. You can do some extensive learning to figure out why it will always be True
even if the user inputted "No".
Upvotes: 1
Reputation: 134
def writeinfo():
a=open('names.txt','wt')
a.write(information)
a.close()
the "information" needs to be passed into the writeinfo function
should be:
def writeinfo(information):
a=open('names.txt','wt')
and above, when the function is called:
print("What would you like to write in the file? \n")
information=input()
writeinfo(information)
Upvotes: 1