我有两个清单:
list_values = [['James', '40', 'Engineer'], ['Mary', '30', 'Nurse]]
Run Code Online (Sandbox Code Playgroud)
和
keys = ['Name', 'Age' 'Profession']。
我的目的是创建一个具有以下输出的字典:
{'name': 'James', 'Age': '40', 'Profession': 'Engineer'},{'name': 'Mary', 'Age': '30', 'Profession': 'Nurse'}
Run Code Online (Sandbox Code Playgroud)
但是,我的代码只提供生成的第一个字典中的内容,即:
{'name': 'James', 'Age': '40', 'Profession': 'Engineer'}
Run Code Online (Sandbox Code Playgroud)
这是代码:
dict_content = {}
for person in list_values:
for val in range(len(keys)):
key = keys[val]
value = person[val]
dict_content[key] = value
print(dict_content)
Run Code Online (Sandbox Code Playgroud)
例如,我不知道为什么我的代码只运行内部for loop,而不返回选择第一个中的第二个嵌套列表for loop。我有兴趣知道为什么我的代码是错误的,因为我花了几乎一天的时间试图解决这个问题,但没有成功。
当我运行程序并输入性别作为其他任何东西但不是男性或女性时,该程序不能按预期工作.我已经挣扎了一天,无法弄清楚.另外如何在调用之后放置函数并仍然使程序运行?我读了一个main函数,但无法弄清楚如何用它修改代码.
from sys import exit
birthdays = {'Alice': 'April 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
def dateOfBirth():
bday = raw_input("> ")
birthdays[name] = bday
print "Database records updated."
print "The number of records is now " + str(len(birthdays)) + "."
print birthday`enter code here`s
exit(0)
while True:
print "Enter a name: (blank to quit)"
name = raw_input("> ")
if name == '':
exit(0)
if name in birthdays:
print birthdays[name] + " is the birthday of " + name + …Run Code Online (Sandbox Code Playgroud)