[英]Converting user input to variable names (python 2.7)
Let's say Bob enters his name, 'Bob Jones,' into a Tkinter text entry field. 假设Bob在Tkinter文本输入字段中输入了他的名字“ Bob Jones”。 Later, I want to be able to access his name like this:
以后,我希望能够像这样访问他的名字:
BobJones = {
'name':'Bob Jones',
'pet\'s name':'Fido'
}
How do I make it so I assign whatever Bob inputs as a new variable without having to manually write it in? 我如何做到这一点,所以我无需手动将Bob输入的任何内容分配为新变量?
Thanks in advance. 提前致谢。
To expand on the comments above, I think you'd want something more like this: 为了扩展上面的评论,我想您想要更多类似的东西:
# Define the user class - what data do we store about users:
class User(object):
def __init__(self, user_name, first_pet):
self.user_name = user_name
self.first_pet = first_pet
# Now gather the actual list of users
users = []
while someCondition:
user_name, first_pet = getUserDetails()
users.append(User(user_name, first_pet))
# Now we can use it
print "The first users name is:"
print users[0].user_name
So you're defining the class (think of it as a template) for the Users, then creating new User objects, one for each person, and storing them in the users
list. 因此,您要为用户定义类(将其视为模板),然后创建一个新的用户对象(每个人一个),并将其存储在
users
列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.