试图让用户可以添加姓名,得分以及用户在高分榜上完成游戏的难度。
def teacher_page():
global scores, name, difficulties
t_choice = None
while t_choice !="0":
print("\nWhat would you like to do?")
print(
"""
0 - Main Menu
1 - Add a score
2 - Remove a score
3 - View highscores
"""
)
t_choice = input("Choice: ")
print()
#exit
if t_choice == "0":
main_menu()
#add a score
elif t_choice == "1":
names = input("Name of the new user to add?\n")
name.append(names)
score = input("What did the user score?\n")
scores.append(score)
difficulty = input("And which difficulty did they complete it on?\n")
difficulties.append(difficulty)
#remove a score
elif t_choice == "2":
names = input("Name of the user you want to remove?\n")
if names in name:
name.remove(names)
score = int(input("What did they score?\n"))
if score in scores:
scores.remove(score)
#view highscores
elif t_choice == "3":
print("High Scores:")
for score in scores:
print(name, score, "on the difficulty ", difficulties)
#if the t_choice does not = to 0,1,2,3
else:
print("Sorry but", t_choice, "isn't a vaild choice.")
但是每次我想在列表中添加用户时都会收到错误消息
AttributeError: 'str' object has no attribute 'append'
我看了几个例子,但不知道我哪里出错了。
答案 0 :(得分:1)
将变量初始化为声明下方的列表。
默认情况下,当您第一次为其分配原始输入时,它们将成为字符串。
做类似的事情:
global scores, name, difficulties
scores=[]
name=[]
difficulties=[]
在全球宣布期间。无需在函数内重新初始化。