使用预先输入的信息定义变量

时间:2014-04-03 02:57:33

标签: python-3.x

所以我有这个代码,但我想这样做,以便一旦输入人员详细信息,我可以插入他们的名称来检索他们的详细信息,例如联系人列表或向人们发送电子邮件的参考所以我记得他们是谁。例如Ill type someones全名(Jennifer Craig)并获得预先输入的详细信息列表。请求帮助。                                                  诚挚                                                      Elemere

import time

def createIdentity():
    print ("Please Enter your details below")
    time.sleep(1)
    name = input("What is your name?")
    time.sleep(1)
    age = input("How old are you?")
    time.sleep(1)
    gender = input("Are you male or female?")

def recallIdentity():
    print("Your name is " + str(name) + "you are " + str(age) + "And you are a " +     str(gender) + "!")


createIdentity()
recallIdentity()

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找dictionary。您可以使用它来存储密钥,并将值映射到它们。

演示 - 你可以检索这样的值:

>>> contacts = {'Jennifer Craig': [30, Female], 'John Doe': [40, 'Male']}
>>> contacts['Jennifer Craig']
[30, Female]

然后像这样添加:

>>> name = 'Mark Smith'
>>> age = 25
>>> gender = 'Male'
>>> contacts[name] = [age, gender]
>>> contacts
{'Jennifer Craig': [30, Female], 'Mark Smith': [25, 'Male'], 'John Doe': [40, 'Male']}