【问题标题】:Python - How can I save the player's score as they keep increasing it? Any help is appreciatedPython - 我如何在玩家不断增加分数时保存他们的分数?任何帮助表示赞赏
【发布时间】:2021-10-20 02:16:06
【问题描述】:

为了学习 Python,我正在开发一个小型终端升级游戏。用户输入一个与随机整数相加的数字以获得分数(在游戏中称为“电影制作”)。我似乎无法解决的问题是每次播放器进入菜单并返回输入更多数字时,前一个数字被删除而不是添加到新数字中。

代码如下:

print("TERMINAL FILM by Dominick")
print("---------------------")

# SCORE
def filmClicker():
    global score 
    user_input = int(input(">> Enter a number: "))
    score = user_input

    if user_input > 5 or user_input < 0 or user_input == 0:
        print(">> Not a valid number.")
        filmClicker()
    elif score > 0:
        score = score + random.randint(1, 50) 
        print("")
        print(">> You produced:", score, "films", "<<")

        go_back_or_menu = input(">> Press ENTER to go again. Or type TAB to go back to the menu. ")
        
        if go_back_or_menu == "":
            filmClicker()
        elif go_back_or_menu == "TAB" or "Tab" or "tab": 
            game()


def game():
    print(">>>>>>>>>>>>> Menu >>>>>>>>>>>>>")
    print(">> Type A to go make films. ")
    print(">> Type B to see your current balance. ")
    print(">> Type C to see the current box office. ")
    print(">> Type D for your stats. ")

    press_button_menu = input("")

    if press_button_menu == "A":
        filmClicker()
    elif press_button_menu == "B":
        print("Current Balance =", score)
        press_enter()
        game()
    else: 
        filmClicker()

game()

所以我希望玩家能够插入一个数字,计算机将这个数字与另一个数字相加,然后吐出一个最终数字。我得到了所有的工作。但是当你多次这样做时它不会保存。这是我不想要的,我希望它在你每次做的时候都堆叠起来。

对不起,如果我解释得不好,如果需要,我可以回答更多。任何帮助表示赞赏。

更新: 我删除了 score = "input" 变量并从任何函数中声明了它。但它仍然没有保存。对于我想要它做什么,这是一个更好的答案:

在下图中,我从游戏菜单开始。然后我决定拍电影,我做了5次。但是当我回到菜单并检查我的余额时,余额等于我最后一次制作电影而不是 TOTAL 电影。我想要它做的是把所有的电影加起来。所以在这种情况下 48 + 49 + 9 + 38 + 25 而不是只有最后一组(在这种情况下是 25),以获得可以通过转到菜单并输入“B”来显示的总余额。

这是当前代码:

import random

score = 0

# SCORE
def filmClicker():
    global score 
    user_input = int(input(">> Enter a number: "))

    if user_input > 5 or user_input < 0 or user_input == 0:
        print(">> Not a valid number.")
        filmClicker()
    elif score > 0:
        score = score + random.randint(1, 50) 
        print("")
        print(">> You produced:", score, "films", "<<")

        go_back_or_menu = input(">> Press ENTER to go again. Or type TAB to go back to the menu. ")
        print(go_back_or_menu)
        
        if go_back_or_menu == "":
            filmClicker()
        elif go_back_or_menu == "TAB" or "Tab" or "tab": 
            game_menu()

# GAME MENU
def game_menu():
    print(">>>>>>>>>>>>> Menu >>>>>>>>>>>>>")
    print(">> Type A to go make films. ")
    print(">> Type B to see your current balance. ")
    print(">> Type C to see the current box office. ")
    print(">> Type D for your stats. ")

    press_button_menu = input("")

    if press_button_menu == "A":
        filmClicker()
    elif press_button_menu == "B":
        print("Current Balance =", score)
        press_enter()
        game_menu()
    else: 
        filmClicker()

game_menu()

第二次更新:

更新代码:

import random

score = 0

# PRINT BLANK LINE
def press_enter():
    press_enter = print(input(""))

# SCORE
def filmClicker():
    global score 
    user_input = int(input(">> Enter a number: "))
    score += user_input
    produced = random.randint(1, 50) 

    if user_input > 5 or user_input < 0 or user_input == 0:
        print(">> Not a valid number.")
        filmClicker()
    elif score > 0:
        score += produced
        print("")
        print(">> You produced:", produced, "films", "<<")

        go_back_or_menu = input(">> Press ENTER to go again. Or type TAB to go back to the menu. ")
        print(go_back_or_menu)
        
        if go_back_or_menu == "":
            filmClicker()
        elif go_back_or_menu == "TAB" or "Tab" or "tab": 
            game_menu()


    # GAME MENU
    def game_menu():
        print(">>>>>>>>>>>>> Menu >>>>>>>>>>>>>")
        print(">> Type A to go make films. ")
        print(">> Type B to see your current balance. ")
        print(">> Type C to see the current box office. ")
        print(">> Type D for your stats. ")
    
        press_button_menu = input("")
    
        if press_button_menu == "A":
            filmClicker()
        elif press_button_menu == "B":
            print("Current Balance =", score)
            press_enter()
            game_menu()
        else: 
            filmClicker()
    
    game_menu()

在下面的图片中,它打印了玩家从该回合产生的产量,但我也在测试分数(这就是 6 和 49 的含义)。它的缩放比例很奇怪,每转一圈都会增加一定的数量。有什么办法解决这个问题?

【问题讨论】:

    标签: python


    【解决方案1】:

    我认为您正在寻找 += 运算符。

    score = 0 保留在文件顶部以初始化变量,然后使用score += user_input 保持运行计数。这和写score = score + user_input是一样的。

    【讨论】:

    • 哇,这确实有效。非常感谢!我有最后一个问题,如果这很好。是否可以显示一个回合但仅在该回合制作的电影数量?就像在您输入数字后显示“已制作 X 部电影”时一样,我希望它仅显示在该特定回合中制作了多少部电影。现在,每次它说制作的电影时,它都会不断堆叠。因此,例如,如果我转了一圈,我希望它只说电影是在那一圈产生的。不是在以前的。但我仍然可以通过菜单访问总数。
    • @Dominick 您可以将随机生成的电影数量存储在单独的变量produced = random.randint(1, 50) 中,使用它来代替随机调用score += produced,并打印produced 而不是score
    • @GeeTransit 我似乎已经明白了,但每次我检查余额时都会得到这个奇怪的 4 加法。我在帖子底部添加了更新后的代码。
    • @Dominick 那是因为您正在做score += user_input 并且您正在使用 4 作为输入进行测试。在问题的屏幕截图中,它比预期高 2 (6 + 41 == 47),因为您输入了 2。
    • @GeeTransit 啊哈,好吧。有没有办法在回合结束时不添加该输入?
    【解决方案2】:

    在您的 filmClicker 函数中,您应该删除以下行:

    score = user_input
    

    通过分配给它,您基本上已经擦除了它之前的值,这就是它不会在轮次之间累积的原因。

    【讨论】:

    • 我这样做了,但是当我输入一个数字时它说“未定义分数”。有什么办法可以解决这个问题?
    • @Dominick 您可以在程序的开头(在任何函数之外)添加score = 0 来初始化它并修复错误。
    【解决方案3】:

    为了保存数据,你可以在python中使用文件

    为了节省:

    f=open("data_game.txt","w")
    f.write(<your score>)
    f.close()
    

    阅读:

    f=open("data_game.txt","r")
    score=f.read()
    print(score)
    f.close()
    

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      相关资源
      最近更新 更多