我对python很新。根据我的理解,您需要为循环块提供一致的选项卡或空间。我相信我有。然而在这个程序的第36行,我一直遇到错误。谁能指出我在这里做错了什么?错误消息告诉我间距不一致。
#High Scores
#Demonstrates list methods
scores=[]
choice = None
while choice!="0":
print(
"""
High Scores
0 - Exit
1 - Show Scores
2 - Add a scores
3 - Delete a Scores
4 - Sort Scores
"""
)
choice = input("Choice: ")
print()
#exit
if choice == "0":
print("Good-bye.")
#list high score-table'
elif choice == "1":
print("High Scores")
for score in scores:
print(score)
#add a score
elif choice == "2":
score = int(input("What score did you get?: "))
scores.append(score)
#remove a score
elif choice == "3":
score = int(input("Remove which score?: "))
if score in scores:
scores.remove(score)
else:
print(score,"isn't in the high scores list.")
#sort scores
elif choice == "4":
scores.sort(reverse=True)
#some unknown choice
else:
print("Sorry, but ", choice, "isn't a valid choice.")
input("\n\nPress the enter key to exit.")
答案 0 :(得分:4)
:
结尾的语句)时,都会堆叠一个缩进。您可以不在其他地方添加缩进。让我们检查您的代码。一切都很好,直到你到达第35行。
34. elif choice == "2":
35. score = int(input("What score did you get?: "))
36. scores.append(score)
当选择为"2"
时,应该询问用户输入。你在第34行有一个缩进,第35行有四个,第36行有两个。你应该有1-2-2
。
34. elif choice == "2":
35. score = int(input("What score did you get?: "))
36. scores.append(score)
第42和44行也是如此:
38. #remove a score
39. elif choice == "3":
40. score = int(input("Remove which score?: "))
41. if score in scores:
42. scores.remove(score)
43. else:
44. print(score,"isn't in the high scores list.")
而不是1-1-2-4-6-4-6
,您需要1-1-2-2-3-2-3
:
38. #remove a score
39. elif choice == "3":
40. score = int(input("Remove which score?: "))
41. if score in scores:
42. scores.remove(score)
43. else:
44. print(score,"isn't in the high scores list.")
同样,对于第48和52行:
46. #sort scores
47. elif choice == "4":
48. scores.sort(reverse=True)
49.
50. #some unknown choice
51. else:
52. print("Sorry, but ", choice, "isn't a valid choice.")
您放置2-2-4-2-2-2-4
,而if
阻止您继续使用elif
从缩进1
开始。您需要1-1-2-1-1-1-2
:
46. #sort scores
47. elif choice == "4":
48. scores.sort(reverse=True)
49.
50. #some unknown choice
51. else:
52. print("Sorry, but ", choice, "isn't a valid choice.")
在OP second revision 之后编辑(感谢@ elParaguayo):
在第31行,for
循环的主体为空:
27. #list high score-table'
28. elif choice == "1":
29. print("High Scores")
30. for score in scores:
31. print(score)
我看了1-1-2-2-2
,它应该是1-1-2-2-3
(与初始版本一样):
27. #list high score-table'
28. elif choice == "1":
29. print("High Scores")
30. for score in scores:
31. print(score)
第35行没有问题。
第38-44区块变为1-1-2-3-4-3-4
,但核心工作仍然适用。
删除了阻止46-52
上的问题。
答案 1 :(得分:0)
提示:使用IDE
问题是你在这个区块中有两个级别的缩进:
elif choice == "2":
score = int(input("What score did you get?: "))
scores.append(score)
从技术上讲,只要您保持一致,使用制表符或空格以及使用的数量并不重要。当然,根据Python style guide,强烈倾向于四个空格。显然Google使用两个空格!
所以你必须修复这段代码。这意味着:
elif choice == "2":
score = int(input("What score did you get?: "))
scores.append(score)
IDE会发现许多缩进错误
从屏幕截图中可以看出,IDE会标记问题行...并节省大量时间。这个特殊的屏幕截图来自Komodo IDE,但大多数支持Python语法的IDE应该会给你类似的东西。
答案 2 :(得分:0)
只是添加其他答案。这看起来也错了:
#list high score-table'
elif choice == "1":
print("High Scores")
for score in scores:
print(score)
应该是
#list high score-table'
elif choice == "1":
print("High Scores")
for score in scores:
print(score)