【发布时间】:2015-10-31 10:36:48
【问题描述】:
我正在创建一个投票系统来选择一个团队,但是我碰壁了。代码运行良好,直到decision2。有什么想法吗?
请记住,此代码正在进行中。
teamNames = []
teams = {}
easy = [0, 1, 2, 3, 4, 5]
NoVotes1 = {'first' : 0, 'second' : 0, 'third' : 0, 'fourth' : 0, 'fifth' : 0}
NoVotes2 = {'first' : 0, 'second' : 0, 'third' : 0, 'fourth' : 0, 'fifth' : 0}
NoVotes3 = {'first' : 0, 'second' : 0, 'third' : 0, 'fourth' : 0, 'fifth' : 0}
NoVotes4 = {'first' : 0, 'second' : 0, 'third' : 0, 'fourth' : 0, 'fifth' : 0}
NoVotes5 = {'first' : 0, 'second' : 0, 'third' : 0, 'fourth' : 0, 'fifth' : 0}
while True:
print("Enter team name " + str(len(teamNames) + 1) + (" or press enter to stop."))
name = input()
if name == "":
break
teamNames = teamNames + [name]
print("The team names are ")
for name in teamNames:
print(" " + name)
for name in teamNames:
teams[name] = 0
teamNames.sort()
print("In alphabetical order the team names are ")
print()
print(str(teamNames))
y = 0
decision1 = input("Would you like to enter a vote? (1 = yes, 2 = no)")
while decision1 == "1":
print("ok great, where did " + teamNames[y] + " come")
decision2 = input()
if decision2 == "1":
teams[easy[y]] = teams[easy[y]] + 1
y = y + 1
if decision1 == "2":
break
elif n == "6":
break
【问题讨论】:
-
你遇到了什么问题?
-
究竟什么是问题所在?您是否正确缩进了
while循环? -
它似乎不喜欢我写的部分 if decision2 == "1": teams[easy[y]] = teams[easy[y]] + 1 。当我运行代码时,它只会显示“teams[easy[y]] = teams[easy[y]] + 1 KeyError: 0”
-
这意味着
teams没有键0,这是有道理的,因为键应该是名称。你期望它如何工作?此外,您确实必须解决问题中的缩进。 -
@AlexHall 抱歉,我只是直接从 python 复制并粘贴了它,团队应该是一个字典,其中包含用户在开始时输入的所有团队,然后当它进入决策 2 时,如果用户键入 1 ,程序应将值 1 添加到字典 teams 中的团队 (y)。