在Python程序中,我创建了一个类userdetails-(尽管我还有一些其他类要在后面添加)。我已经从该类创建了2个对象,但我不能调用所有对象?这是我的问题。我想将单独的pin_no链接到单独的对象,并单独调用它们的详细信息
from Details import userdetails
pin_no = (1111, 2222)
while True:
pin_no = input("Input the no : ")
if pin_no == '1111' or pin_no == '2222':
print ("\n Hello and welcome to my program. Please choose from one of the following options:")
break
else:
print ("please try again ")
newuserdetails = userdetails ('Tom', '23 Bishop St ', 'cv3_f45', 2347477472)
newuserdetails1 = userdetails ('Bill', '81 Oliver St ', 'CV6 7FR', 574747477)
user = input("\n\n 1. Userdetails \n 2. Address \n 3. Post Code \n 4. Tel No " '\n')
a = '1'
b = '2'
c = '3'
d = '4'
if user == a:
print (newuserdetails.name) or (newuserdetails1.name)
elif user == b:
print (newuserdetails.address)
elif user == c:
print (newuserdetails.post_code)
elif user == d:
print (newuserdetails.tel_no)
我相信你要找的是一本字典
然后,您可以使用以下方法通过pin获取用户详细信息实例:
但是,我警告不要使用pin作为获取用户身份的方式
如果两个用户碰巧选择了相同的pin码,那么就会出现错误,您的代码也会中断。 您更适合使用ID来获取用户详细信息
编辑:
下面是我正在使用的,它在Python3.8上的工作与预期的一样
相关问题 更多 >
编程相关推荐