我正在制作一种有趣的基于文本的RPG,但在添加到“ balance”变量以给玩家更多钱方面遇到了一些麻烦。
balance = int(0)
def step_result(balance):
result = random.randint(1,5)
if result == 1:
time.sleep(0.5)
print("You come across a friendly merchant who gives you 50 gold pieces as a token of friendship")
balance = balance + 50
time.sleep(1)
稍后在代码中,我使用print(balance)来显示余额,但余额仍显示为0。有什么想法吗?
答案 0 :(得分:0)
尝试一下,Global应该解决您的问题
balance = int(0)
def step_result(balance):
result = random.randint(1,5)
global balance
if result == 1:
time.sleep(0.5)
print("You come across a friendly merchant who gives you 50 gold pieces as a token of friendship")
balance = balance + 50
time.sleep(1)