Python 3.x 用Python自动化那些无聊的东西
这个程序来自“用python自动化那些无聊的东西”。我无法理解,是什么告诉程序将“name”作为键,将“bday”作为值Python 3.x 用Python自动化那些无聊的东西,python-3.x,Python 3.x,这个程序来自“用python自动化那些无聊的东西”。我无法理解,是什么告诉程序将“name”作为键,将“bday”作为值 birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'} while True: print('Enter a name: (blank to quit)') name = input() if name == '': break
birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
while True:
print('Enter a name: (blank to quit)')
name = input()
if name == '':
break
if name in birthdays:
print(birthdays[name] + ' is the birthday of ' + name)
else:
print('I do not have birthday information for ' + name)
print('What is their birthday?')
bday = input()
birthdays[name] = bday
print('Birthday database updated.')
我们可以直接向字典添加键、值 供参考:
d = {}
print(d)
d['Name']='1st April';
print(d)
输出:
{}
{'Name': '1st April'}
这里,“名称”是键,而“四月一日”是值
Python从括号[]内识别键,并将值指定为d[键]=值
birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
while True:
print('Enter a name: (blank to quit)')
name = input()
if name == '':
break
if name in birthdays:
print(birthdays[name] + ' is the birthday of ' + name)
else:
print('I do not have birthday information for ' + name)
print('What is their birthday?')
bday = input()
birthdays[name] = bday
print('Birthday database updated.')
要查看字典中的详细信息,请参阅:我们可以直接向字典中添加键、值 供参考:
d = {}
print(d)
d['Name']='1st April';
print(d)
输出:
{}
{'Name': '1st April'}
这里,“名称”是键,而“四月一日”是值
Python从括号[]内识别键,并将值指定为d[键]=值
birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
while True:
print('Enter a name: (blank to quit)')
name = input()
if name == '':
break
if name in birthdays:
print(birthdays[name] + ' is the birthday of ' + name)
else:
print('I do not have birthday information for ' + name)
print('What is their birthday?')
bday = input()
birthdays[name] = bday
print('Birthday database updated.')
要查看字典中的详细信息,请参阅:您可以阅读dict文档: 行
a_字典年[xxx]=yyy
xxx
命名为键,yyy
命名为值您可以阅读dict文档:
行a_字典年[xxx]=yyy
xxx
被命名为键,yyy
被命名为值第9行-如果生日中有姓名:
当使用关键字中的对列表进行检查而不向字典添加任何方法时,它会自动检查其键
例如,如果birthdays
中的name
与birthdays.keys()
中的条目相同,则将bday
设置为用户输入
当它像birthdays[name]=bday那样使用时,就像在字典birthdays
中添加一个键值对,如果birthdays中的name:
当使用
关键字中的对列表进行检查而不向字典添加任何方法时,它会自动检查其键
例如,如果birthdays
中的name
与birthdays.keys()
中的条目相同,则将bday
设置为用户输入
当它像birthdays[name]=bday
那样使用时,就像是在字典birthdays
中添加一个键值对