我正在为我的游戏开发人员完成一项家庭作业。上课和我无法弄清楚我做错了什么。我试图通过使用用户输入将数字添加到已经硬连线的高分中。例如;试图给目前拥有3456分的罗杰增加100分。我只是还没弄清楚我做错了什么让代码工作。所有和任何帮助非常感谢。谢谢。
str1 = ["Roger", 3456]
str2 = ["Justin", 2320]
str3 = ["Beth", 1422]
enter = 0
start = 0
Roger = ()
def incdec(sco):
return addit
def addition(num1):
return num1 + num1
def square(num):
print("I'm in square")
return num * num
def display(message):
"""Display game instuctions"""
print(message)
def instructions():
"""Display game instuctions"""
print("Welcome to the world's greatest game")
def main():
instructions()
scores = [str1, str2, str3]
start = input("Would you like to view the high score options? y/n ")
if start == "y":
print("""\
Hello! Welcome to the high scores!
Here are the current high score leaders!:
""")
print(scores)
print("""\n\
0 - Sort high scores
1 - Add high score
2 - Reverse the order
3 - Remove a score
4 - Square a number
5 - Add 2 numbers together
6 - Add to a score
7 - Subtract from a score
""")
option = int(input("Please enter your selection "))
while option < 8:
print(scores)
if option == 0:
scores.sort()
print("These are the scores sorted alphabetically")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 1:
print(scores)
print("Please enter your name and score; After entering your name, hit the return key and enter your score")
name = input()
score = int(input())
entry = (name,score)
scores.append(entry)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 2:
print(scores)
scores.reverse()
print("\nHere are the scores reversed")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 3:
print(scores)
print("Please enter the high score you would like to remove. After typing the name, hit the return key and enter the score")
name1 = input()
score1 = int(input())
remove = (name1,score1)
scores.remove(remove)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 4:
val = int(input("Give me a number to square"))
sqd = square(val)
print(sqd)
option = option = int(input("Please enter your selection"))
elif option == 5:
val0 = int(input("Give me one number"))
val1 = int(input("Give me another number"))
addi = (val0 + val1)
print(addi)
option = option = int(input("Please enter your selection"))
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
addit =(sc0 + str(sc1))
print(addit)
option = option = int(input("Please enter your selection"))
elif option == 7:
break
main()
答案 0 :(得分:0)
您正在为字符串添加数字。您需要做的是在scores
列表中搜索播放器,然后将给定的数字添加到其当前分数中。使用当前结构,您可以执行以下操作:
def update_score(scores, person, amount):
# Loop through the current scores
for score in scores:
# Find the person we're looking for
if score[0].lower() == person.lower():
# Update their score
score[1] += amount
return scores
然后修改:
...
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
scores = update_score(scores, sc0, sc1)
print(scores)
option = option = int(input("Please enter your selection"))
...
为我制作的:
Please enter the person whose score you would like to add to roger
Please enter the amount you would like to add to their score 100
[['Roger', 3556], ['Justin', 2320], ['Beth', 1422]]
然而,我会重构很多样板并使用字典查找来跟踪玩家。但上述解决方案解决了您所述的问题。