【问题标题】:Trying to increase a high score by user input in python试图通过python中的用户输入来增加高分
【发布时间】:2015-02-14 07:25:36
【问题描述】:

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

【问题讨论】:

  • 你能详细说明什么不起作用吗?也许一些示例输入和输出?
  • 选项 6。假设我想在 Roger 当前的 3456 上加 100 分。根据代码当前的设置方式,它只会打印出 'Roger', 100 @JoshuaGilman
  • 我可能会从实际使用变量scores 开始

标签: python function


【解决方案1】:

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

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

【讨论】:

  • 没关系,我想通了。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 2011-12-05
相关资源
最近更新 更多