Python user data check -
i have program gets user's input , asks whether information correct or not. however, when user has finished entering information, error code:
#welcome print("welcome game!\n") def data(): #age age = int(input("enter age: ")) #gender gen = input("enter gender: ") #email mail = input("enter email: ") #username name = input("enter name: ") return (age, gen, mail, name) def datacheck(): print("your information:\n") print("age: ", age, "\n") print("gender: ", gen, "\n") print("email: ", mail, "\n") print("username: ", name, "\n") yn = input("is correct? yes or no: ") if yn == "yes": print("hello, ", name) if yn == "no": data() #array variables age = data[0] gen = data[1] mail = data[2] name = data[3] #datacheck datacheck() data = data() #array variable. age = data[0] gen = data[1] mail = data[2] name = data[3] #datacheck datacheck()
the error is:
traceback (most recent call last): file "/users/joe/documents/test123.py", line 48, in datacheck() file "/users/joe/documents/test123.py", line 20, in datacheck print("age: ", age, "\n") unboundlocalerror: local variable 'age' referenced before assignment
there no error if type re-entering of info doesn't work:
#welcome print("welcome game!\n") def data(): #age age = int(input("enter age: ")) #gender gen = input("enter gender: ") #email mail = input("enter email: ") #username name = input("enter name: ") return (age, gen, mail, name) def datacheck(): print("your information:\n") print("age: ", age, "\n") print("gender: ", gen, "\n") print("email: ", mail, "\n") print("username: ", name, "\n") yn = input("is correct? yes or no: ") if yn == "yes": print("hello, ", name) if yn == "no": data() #datacheck datacheck() data = data() #array variable. age = data[0] gen = data[1] mail = data[2] name = data[3] #datacheck datacheck()
how user can re-enter information (data function)?
age, gen, mail, name local variables
in both data()
, datacheck()
, variables scope
method belong to. have define variables global
or pass variables/tuple object 1 method another.
based on logic, need pass tuple object datacheck()
, modify datacheck(tuple)
, tested follows:
hzhang@dell-work ~/pycharmprojects/test $ cat test.py #welcome print("welcome game!\n") def data(): #age age = int(input("enter age: ")) #gender gen = input("enter gender: ") #email mail = input("enter email: ") #username name = input("enter name: ") return (age, gen, mail, name) def datacheck(data): age = data[0] gen = data[1] mail = data[2] name = data[3] print("your information:\n") print("age: ", age, "\n") print("gender: ", gen, "\n") print("email: ", mail, "\n") print("username: ", name, "\n") yn = input("is correct? yes or no: ") if yn == "yes": print("hello, ", name) if yn == "no": values = data() #datacheck datacheck(values) data = data() #array variable. age = data[0] gen = data[1] mail = data[2] name = data[3] #datacheck datacheck(data)hzhang@dell-work ~/pycharmprojects/test $ python test.py welcome game! enter age: 22 enter gender: "male" enter email: "hi@email.com" enter name: "haifzhan" information: ('age: ', 22, '\n') ('gender: ', 'male', '\n') ('email: ', 'hi@email.com', '\n') ('username: ', 'haifzhan', '\n') correct? yes or no: "yes" ('hello, ', 'haifzhan')
Comments
Post a Comment