python - Python - 我如何在玩家不断增加分数时保存他们的分数?任何帮助表示赞赏
问题描述
学习 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 的意思)。它的缩放比例很奇怪,每转一圈都会增加一定的数量。有任何解决这个问题的方法吗?
解决方案
我认为您正在寻找+=
运营商。
保持score = 0
文件顶部的 初始化变量,然后使用score += user_input
保持运行计数。这和写作一样score = score + user_input
。
推荐阅读
- node.js - 获取 PNG 图像时出现意外的缓冲区结果
- mongodb - MongoDB中的大数据是什么?
- angular - 找不到 API 编译器-cli,函数 __NGTOOLS_PRIVATE_API_2
- angular - 重新加载页面 (ngSubmit) 不起作用
- angularjs - 用 Jasmine 测试 Token 是否存储在 localStorage
- php - mysql_connect 不再工作,如何用 mysqli 修复?
- ffmpeg - 如何在使用 ffmpeg 创建 mp4 作为输出时循环 gif 文件有限次数
- html -
正在为每个空间创造利润 - python - 如何多次使用函数的结果作为同一个函数的参数?
- r - 为什么 x 轴标签显示 df[,1] 而不是列名?