Python - AttributeError: 'str' object has no attribute 'append' - maths game
Python - AttributeError: 'str' object has no attribute 'append' - maths game
正在努力让用户可以在高分列表中添加姓名、分数以及用户完成游戏的难度。
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'
我看过几个示例,但不确定哪里出错了。
将您的变量初始化为 lists,就在它们的声明下方。
默认情况下,当您第一次将原始输入分配给它们时,它们会变成字符串。
做类似的事情:
global scores, name, difficulties
scores=[]
name=[]
difficulties=[]
全局声明中。无需在函数内再次初始化
正在努力让用户可以在高分列表中添加姓名、分数以及用户完成游戏的难度。
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'
我看过几个示例,但不确定哪里出错了。
将您的变量初始化为 lists,就在它们的声明下方。
默认情况下,当您第一次将原始输入分配给它们时,它们会变成字符串。
做类似的事情:
global scores, name, difficulties
scores=[]
name=[]
difficulties=[]
全局声明中。无需在函数内再次初始化