试图通过python中的用户输入提高高分
我正在为我的游戏开发课做家庭作业,我不太明白我做错了什么。我正在尝试将数字添加到一个高分中,这个分数已经通过用户输入硬连接在一起了。例如试图给罗杰加100分,他目前有3456分。我只是还没有完全弄清楚我在做什么错误,让代码正常工作。非常感谢您的帮助。多谢各位试图通过python中的用户输入提高高分,python,function,Python,Function,我正在为我的游戏开发课做家庭作业,我不太明白我做错了什么。我正在尝试将数字添加到一个高分中,这个分数已经通过用户输入硬连接在一起了。例如试图给罗杰加100分,他目前有3456分。我只是还没有完全弄清楚我在做什么错误,让代码正常工作。非常感谢您的帮助。多谢各位 str1 = ["Roger", 3456] str2 = ["Justin", 2320] str3 = ["Beth", 1422] enter = 0 start = 0 Roger = () def incdec(sco):
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()
str1=[“收到”,3456]
str2=[“贾斯汀”,2320]
str3=[“贝思”,1422年]
输入=0
开始=0
罗杰=()
def incdec(上海合作组织):
返回地址
def添加量(num1):
返回num1+num1
def平方(num):
打印(“我在广场上”)
返回num*num
def显示(信息):
“”“显示游戏说明”“”
打印(信息)
def指令():
“”“显示游戏说明”“”
打印(“欢迎来到世界上最伟大的游戏”)
def main():
指示()
分数=[str1、str2、str3]
开始=输入(“您想查看高分选项吗?是/否”)
如果开始==“y”:
打印(“”)\
你好!欢迎来到高分!
以下是当前的高分领导者!:
""")
打印(分数)
打印(“”\n\
0-排序高分
1-加高分
2-颠倒顺序
3-删除分数
4-将一个数字平方
5-将两个数字相加
6-加分
7-从分数中减去
""")
option=int(输入(“请输入您的选择”))
选项<8时:
打印(分数)
如果选项==0:
scores.sort()
打印(“这些是按字母顺序排列的分数”)
打印(分数)
option=option=int(输入(“请输入您的选择”))
elif选项==1:
打印(分数)
打印(“请输入您的姓名和分数;输入姓名后,按回车键并输入您的分数”)
名称=输入()
分数=整数(输入()
条目=(姓名、分数)
分数。附加(条目)
打印(分数)
option=option=int(输入(“请输入您的选择”))
elif选项==2:
打印(分数)
分数。反向()
打印(\n分数是否颠倒)
打印(分数)
option=option=int(输入(“请输入您的选择”))
elif选项==3:
打印(分数)
打印(“请输入要删除的高分。键入名称后,按回车键并输入分数”)
名称1=输入()
score1=int(输入())
删除=(名称1,分数1)
分数。删除(删除)
打印(分数)
option=option=int(输入(“请输入您的选择”))
elif选项==4:
val=int(输入(“给我一个平方数”))
sqd=平方(val)
打印(sqd)
option=option=int(输入(“请输入您的选择”))
elif选项==5:
val0=int(输入(“给我一个数字”))
val1=int(输入(“给我另一个数字”))
addi=(val0+val1)
打印(addi)
option=option=int(输入(“请输入您的选择”))
elif选项==6:
sc0=输入(“请输入您想要添加分数的人”)
sc1=int(输入(“请输入您希望添加到其分数中的金额”))
加法=(sc0+str(sc1))
打印(附加)
option=option=int(输入(“请输入您的选择”))
elif选项==7:
打破
main()
您正在向字符串添加一个数字。您需要做的是在
分数列表中搜索玩家,然后将给定的数字添加到他们的当前分数中。使用当前结构,您可以执行以下操作:
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]]
然而,我会重构很多样板文件,并使用字典查找来跟踪玩家。但是上述解决方案解决了您所说的问题。您能详细说明哪些不起作用吗?可能是一些示例输入和输出?选项6。假设我想在罗杰目前的3456上加100分。根据代码当前的设置方式,它只会打印回“Roger”,100@JoshuaGilmanI可能会从实际使用变量分数开始,因为无论如何,我都知道了。谢谢你的帮助!