繁体   English   中英

试图通过python中的用户输入来提高分数

[英]Trying to increase a high score by user input in python

我正在为我的游戏开发人员完成一项家庭作业。 上课和我无法弄清楚我做错了什么。 我试图通过使用用户输入将数字添加到已经硬连线的高分中。 例如; 试图给目前拥有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()

您正在为字符串添加数字。 您需要做的是在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]]

但是,我会重构很多样板并使用字典查找来跟踪玩家。 但上述解决方案可以解决您所述的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM