1

I have a program that gets user's input and then asks whether the information is correct or not. However, when the user has finished entering information, I get an error with this code:

#Welcome
print("Welcome to the game!\n")


def data():
    #Age
    age = int(input("Enter your age: "))
    #Gender
    gen = input("Enter your gender: ")
    #email
    mail = input("Enter your email: ")
    #username
    name = input("Enter your 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 it correct? Yes or No: ")
    if yn == "Yes":
        print("Hello, ", name)
    if yn == "No":
        data()
        #Array to variables
        age = data[0]
        gen = data[1]
        mail = data[2]
        name = data[3]
        #datacheck
        datacheck()


data = data()


#Array to Variable.
age = data[0]
gen = data[1]
mail = data[2]
name = data[3]
#datacheck
datacheck()

The error I get 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 is no error if I type but the re-entering of info doesn't work:

#Welcome
print("Welcome to the game!\n")


def data():
    #Age
    age = int(input("Enter your age: "))
    #Gender
    gen = input("Enter your gender: ")
    #email
    mail = input("Enter your email: ")
    #username
    name = input("Enter your 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 it correct? Yes or No: ")
    if yn == "Yes":
        print("Hello, ", name)
    if yn == "No":
        data()
        #datacheck
        datacheck()


data = data()


#Array to Variable.
age = data[0]
gen = data[1]
mail = data[2]
name = data[3]
#datacheck
datacheck()

How would I get it so that the user can re-enter their information (data function)?

bodyfarmer
  • 402
  • 1
  • 7
  • 17
Doseph
  • 199
  • 3
  • 17

4 Answers4

2

age, gen, mail, name are local variables in both data() and datacheck(), those variables scope is the method they belong to. You have to define those variables as global or you pass the variables/tuple object from one method to another.

Based on your logic, you need to pass your tuple object into datacheck(), modify it to datacheck(tuple), I tested it as follows:

hzhang@dell-work ~/PycharmProjects/Test $ cat test.py 
#Welcome
print("Welcome to the game!\n")


def data():
    #Age
    age = int(input("Enter your age: "))
    #Gender
    gen = input("Enter your gender: ")
    #email
    mail = input("Enter your email: ")
    #username
    name = input("Enter your 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 it correct? Yes or No: ")
    if yn == "Yes":
        print("Hello, ", name)
    if yn == "No":
        values = data()
        #datacheck
        datacheck(values)


data = data()

#Array to 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 to the game!

Enter your age: 22
Enter your gender: "male"
Enter your email: "hi@email.com"
Enter your name: "haifzhan"
Your information:

('Age: ', 22, '\n')
('Gender: ', 'male', '\n')
('Email: ', 'hi@email.com', '\n')
('Username: ', 'haifzhan', '\n')
Is it correct? Yes or No: "Yes" 
('Hello, ', 'haifzhan')
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
2

You have a scoping issue.

Furthermore, I think you might want to consider being object oriented. That is, if my assumptions are right about your use of the user data. Instead of holding your user's attributes in a bunch of arrays (lists), you can hold the user's attributes in something called an object. I mean, if your user ends up having 2,000 attributes, do you want to have to remember what attribute number represents their eye color so you can reference it in a list? Probably not. haha

This approach might make your code easier to use/reuse and more organized for the future:

class User():
    """A class representing a user.

    defaults set.
    """

    def __init__(self, data={'age': 1,
                             'gender': 'x',
                             'mail': 'user@default',
                             'name': 'User'}):

        self.data = data
        self.age = self.data['age']
        self.gender = self.data['gender']
        self.mail = self.data['mail']
        self.name = self.data['name']

    def __str__(self):
        """The string representation of a User object."""

        return self.name


    def get_age(self):
        """Return the user's age."""

        return self.age

    def get_gender(self):
        """Return the user's gender."""

        return self.gen

    def get_mail(self):
        """Return the user's email address."""

        return self.mail

    def get_name(self):
        """Return the user's name."""

        return self.name

    def get_data(self):
        """Return a dictionary containing a User's info."""

        return self.data

if __name__=="__main__":

    user_data = {} # will contain our data

    # ask for user input
    for key in ["age", "gender", "mail", "name"]:
        user_data[key] = raw_input("Enter your %s: " % key)

    # Store user input data in an object
    user = User(data=user_data)

    print user.get_data() # prints a dictionary containing user values
Community
  • 1
  • 1
Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
0

Your variables are not global, you need to pass them to the function or declare them global.

Also raw_input should be used instead of input for strings as noted here: Python 2.7 getting user input and manipulating as string without quotations

#Welcome
print("Welcome to the game!\n")


def data():
    #Age
    age = raw_input(input("Enter your age: "))
    #Gender
    gen = raw_input("Enter your gender: ")
    #email
    mail = raw_input("Enter your email: ")
    #username
    name = raw_input("Enter your name: ")
    return (age, gen, mail, name)

def datacheck(age, gen, mail, name):
    print("Your information:\n")
    print("Age: ", age, "\n")
    print("Gender: ", gen, "\n")
    print("Email: ", mail, "\n")
    print("Username: ", name, "\n")
    yn = raw_input("Is it correct? Yes or No: ")
    if yn == "Yes":
        print("Hello, ", name)
    if yn == "No":
        data()
        #datacheck
        datacheck(age, gen, mail, name)


data = data()


#Array to Variable.
age = data[0]
gen = data[1]
mail = data[2]
name = data[3]
#datacheck
datacheck(age, gen, mail, name)

Works for me.

Community
  • 1
  • 1
marsh
  • 2,592
  • 5
  • 29
  • 53
0

You need to pass the variables from the first function to the second on before trying to use them

#Welcome
print("Welcome to the game!\n")


def data():
    age = int(input("Enter your age: "))
    gen = str(input("Enter your gender: "))
    mail = str(input("Enter your email: "))
    name = str(input("Enter your name: "))
    return (age, gen, mail, name)

def datacheck():
    age, gen, mail, name = data()
    print("Your information:\n")
    print("Age: %s\n"%(age))
    print("Gender: %s\n"%(gen))
    print("Email: %s\n"%(mail))
    print("Username: %s\n"%(name))
    yn = input("Is it correct? Yes or No: ")
    if yn == "Yes":
        print("Hello, ", name)
    if yn == "No":
        datacheck()

#datacheck
datacheck()
bladexeon
  • 696
  • 3
  • 10
  • 31