【发布时间】:2020-03-04 21:22:16
【问题描述】:
我正在尝试迭代几个输入,然后填充字典,但是当我输入最后一个键时,每次在最后一个键时我都会收到这个 keyError,它会抛出这个错误。
# Get user input and define our roster
roaster = {}
while True:
position = input('position: ')
player = input('player: ')
roaster[position] = [player]
roaster.update()
if len(roaster) == 1:
break
print(f'Your starting {len(roaster)} for the upcoming basketball season')
print(f'\t\t{roaster[player]}:\t\t{roaster[position]}')
错误:
Traceback (most recent call last):
File "/home/Basketball_Roaster_App/basketball_ball_roaster.py", line 16, in <module>
print(f'\t\t{roaster[player]}:\t\t{roaster[position]}')
KeyError: 'mike'
【问题讨论】:
-
你提供了什么输入来得到这个错误?
-
顺便说一下,roster 的拼写没有'a'
-
update函数是什么?此外,为什么总是只允许一名玩家——break确保了这一点。它是一个非常小的烘焙机吗? -
我正在提供字符串
标签: python dictionary while-loop