简体   繁体   English

将raw_input answer设置为预定义变量

[英]Set raw_input answer as pre-defined variable

Newbie here. 新手在这里。

I wanted to have Python to look through the variables (in this case, john, clark, and bruce) and spit out the strings in those arrays, but I'm having no luck figuring out how. 我想让Python查看变量(在这种情况下,john,clark和bruce)并吐出那些数组中的字符串,但我没有运气弄清楚如何。 Here's the example I was working on: 这是我正在研究的例子:

names = ("john", "clark", "bruce")

john = ("doe", "13-apr-1985")
clark = ("kent", "11-jan—1987")
bruce = ("wayne", "05-sep-1988")

user = raw_input("What is your name?")
if user in names:
  print "Your last name is: " + ????[0]
  print "Your date of birth is: " + ????[1]
else:
  print "I don’t know you."

The question marks is where I am stuck. 问号是我被困的地方。 I don't know how to link the two together. 我不知道如何将两者联系在一起。 I hope my question isn't too confusing. 我希望我的问题不会太混乱。

You can use a dictionary: 你可以使用字典:

names = {
    "john": {
        "last_name": "doe",
        "date_of_birth": "13-apr-1985"
    },
}

user = raw_input("What is your name?")
if user in names:
    print "Your last name is: " + names[user]["last_name"]
    print "Your date of birth is: " + names[user]["date_of_birth"]
else:
    print "I don’t know you."

Note that this approach wouldn't work when multiple people have the same first name. 请注意,当多个人具有相同的名字时,此方法不起作用。 In that case you need to adapt the data structure to handle that accordingly (and you need to decide whose results you're going to display when someone fills in "john" ). 在这种情况下,您需要调整数据结构以相应地处理(并且您需要决定当某人填写"john"时您将显示哪些结果)。

Use a dictionary to map the first names to the tuples you have. 使用字典将名字映射到您拥有的元组。

names = { "john": (“doe”, “13-apr-1985”),
          "clark": (“kent”, “11-jan—1987”),
          "bruce": (“wayne”, “05-sep-1988”)}

user = raw_input(“What is your name?”)
if user in names.keys():
  print “Your last name is: “ + names[user][0]
  print “Your date of birth is: “ + names[user][1]
else:
  print “I don’t know you.”

To make this even more pythonic and easier to work with, make a nested dictionary: 为了使这更加pythonic和更容易使用,创建一个嵌套字典:

names = { "john": {"last": “doe”, "birthdate": “13-apr-1985”},
          "clark": {"last": “kent”, "birthdate": “11-jan—1987”},
          "bruce": {"last": “wayne”, "birthdate": “05-sep-1988”}}

user = raw_input(“What is your name?”)
if user in names.keys():
  print “Your last name is: “ + names[user]["last"]
  print “Your date of birth is: “ + names[user]["birthdate"]
else:
  print “I don’t know you.”

As a side note, you probably want to trim any leading whitespace off the input while you're at it. 作为旁注,您可能希望在输入时修剪输入中的任何前导空格。

...
user = raw_input(“What is your name?”)
user = user.strip()
if user in names.keys():
  ...

You should use a dict here, like this 你应该在这里使用dict ,就像这样

d = {"john" : ("doe", "13-apr-1985"),
     "clark" : ("kent", "11-jan-1987"),
     "bruce" : ("wayne", "05-sep-1988")}

...
...
if user in d:
    print “Your last name is: “ + d[user][0]
    print “Your date of birth is: “ + d[user][0]
else:
    print “I don’t know you.”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM