简体   繁体   中英

My information won't write to a file? - Python

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.

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".

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)

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.

other problems

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.

has the user selected 'yes'?

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 (eg lower-casing it, removing trailing whitespace).

solution

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM