gpt4 book ai didi

Python 用户数据校验

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:10 25 4
gpt4 key购买 nike

我有一个程序可以获取用户的输入,然后询问信息是否正确。但是,当用户输入完信息后,我收到以下代码的错误:

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

我得到的错误是:

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

如果我输入没有错误,但重新输入信息不起作用:

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

我如何获取它以便用户可以重新输入他们的信息(数据功能)?

最佳答案

你有一个 scoping问题。

此外,我认为您可能想考虑成为 object oriented .也就是说,如果我对您使用用户数据的假设是正确的。您可以将用户的属性保存在称为 object 的东西中,而不是将用户的属性保存在一堆数组(列表)中。 .我的意思是,如果您的用户最终拥有 2,000 个属性,您是否需要记住代表他们眼睛颜色的属性编号以便您可以在列表中引用它?可能不会。哈哈

这种方法可能会使您的代码更易于使用/重用,并且在未来更有条理:

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

关于Python 用户数据校验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com