我正在尝试使用用户输入访问代码中的变量。我意识到输入会给我一个字符串而不是变量,但是我不知道如何解决这个问题。我一直收到一条错误:
AttributeError: 'str' object has no attribute 'email'
这是代码。有什么建议?
class Person:
def __init__(self, number, first, last, email):
self.number = number
self.first = first
self.last = last
self.email = email
p1 = Person('1', 'first name', 'last name', 'email address')
p2 = Person('2', 'first name', 'last name', 'email address')
p3 = Person('3', 'first name', 'last name', 'email address')
p5 = Person('5', 'first name', 'last name', 'email address')
p6 = Person('6', 'first name', 'last name', 'email address')
p9 = Person('9', 'first name', 'last name', 'email address')
p10 = Person('10', 'first name', 'last name', 'email address')
user='p'+raw_input('Please Enter ID Number: ')
print(user.email)
答案 0 :(得分:1)
使用字典! (虽然不是实现这一目标的唯一方法,但这是正确的方法(至少是imho))
people = dict(
p1 = Person('1', 'first name', 'last name', 'email address')
p2 = Person('2', 'first name', 'last name', 'email address')
p3 = Person('3', 'first name', 'last name', 'email address')
p5 = Person('5', 'first name', 'last name', 'email address')
p6 = Person('6', 'first name', 'last name', 'email address')
p9 = Person('9', 'first name', 'last name', 'email address')
p10 = Person('10', 'first name', 'last name', 'email address')
)
print people["p"+raw_input("id")]
或使用列表
people = [ Person('1', 'first name', 'last name', 'email address'), Person('1', 'first name', 'last name', 'email address'), Person('1', 'first name', 'last name', 'email address'),...]
print people[int(raw_input("Enter ID:"))]
或更改您的Person
班级
class Person:
people = {}
def __init__(self,id,first,last,email):
Person.people[id] = self
....
@classmethod
def getById(cls,id):
try:
return cls.people[id]
except:
return None
#then Later
p1 = Person('1', 'first name', 'last name', 'email address')
p2 = Person('2', 'first name', 'last name', 'email address')
p3 = Person('3', 'first name', 'last name', 'email address')
p5 = Person('5', 'first name', 'last name', 'email address')
p6 = Person('6', 'first name', 'last name', 'email address')
p9 = Person('9', 'first name', 'last name', 'email address')
p10 = Person('10', 'first name', 'last name', 'email address')
print Person.getById(raw_input("Enter ID:"))
答案 1 :(得分:0)
将Person
实例存储在列表或字典中会更好地方式。但是,您可以通过在globals()
字典中查找用户输入的变量名称来完成您的工作:
class Person:
def __init__(self, number, first, last, email):
self.number = number
self.first = first
self.last = last
self.email = email
p1 = Person('1', 'first name', 'last name', 'email address1')
p2 = Person('2', 'first name', 'last name', 'email address2')
p3 = Person('3', 'first name', 'last name', 'email address3')
p5 = Person('5', 'first name', 'last name', 'email address5')
p6 = Person('6', 'first name', 'last name', 'email address6')
p9 = Person('9', 'first name', 'last name', 'email address9')
p10 = Person('10', 'first name', 'last name', 'email address10')
user = 'p' + raw_input('Please Enter ID Number: ')
try:
user = globals()[user]
except KeyError:
print('ID Number Unknown!')
raise
print(user.email)
答案 2 :(得分:-1)
Python将用户解释为字符串“pX”(其中X是输入),而不是变量pX。要解决此问题,您必须将字符串计算为变量。试试print(eval(user).email)
。这将评估字符串并返回它所引用的Person对象,然后获取email属性。