首页 > 解决方案 > 保存函数中的变量以在循环时重用

问题描述

我昨天才开始学习python,所以完全菜鸟。我有大约 10 小时的 javascript 经验,但由于我在大学学习 Python,因此不得不转换。我决定尝试自己制作一个程序,因为与无数视频相比,我学到了很多东西。

我遇到的主要问题是保存我的平衡变量。当我赢或输时,平衡会在 2000 年重新开始。我认为这可能是因为变量balance = 2000,但如果我不定义它,它就不起作用。我知道这并不重要,但我想知道我哪里出错了。

如果有人可以帮助我,将不胜感激。我也知道代码一团糟,但以后会尝试让它变得更好。

global balance
name = input ("Please Enter Your Name: ")
password = input ("Please Make A Password: ")

def playgame():
 balance = 2000
 yesOrNo = input("Would You Like To Put A Bet on Y/N?; ")
 if yesOrNo == "y":
  howBigABet = int(input("How Much Would You like to bet?: "))
  if howBigABet <= balance:
     pickNumber = int(input("Please PicK A Number: "))
     from random import randint
     value = randint(0, 1)
     print(value)
     if pickNumber == value:
           print(value)
           balance = balance + howBigABet * 2
           print("you won " + str(howBigABet * 2))
           print(balance)
           playgame()
     else:
      print ("Sorry Wrong Number")
      balance = balance - howBigABet
      print("your new balance is: " + str(balance))
      playgame()
  if balance <= 200:
   print("You Are Low on Cash Top up?")
  elif balance <= 0:
   print("You Are Out Of cash " + name + " Top Up ")
   playgame()

 else:
    print("Sorry You Dont Have Enough Money")

print("Hello " + name)
passwordAttempted = input("Please Enter Your Password: ")

if passwordAttempted == password:
    print("Welcome " + name)
    playgame()
else:
    print ("Wrong Password " + name + " !")

标签: python

解决方案


您可以将balance作为参数传递给您的playgame函数

像这样

def playgame(balance=2000):
    yesOrNo = input("Would You Like To Put A Bet on Y/N?; ")
    ...

而不仅仅是传球,所有游戏调用的新平衡

但是比使用循环函数更好的方法是 while 循环

name = input ("Please Enter Your Name: ")
password = input ("Please Make A Password: ")


def playgame(balance=2000):
 yesOrNo = input("Would You Like To Put A Bet on Y/N?; ")
 if yesOrNo == "y":
  howBigABet = int(input("How Much Would You like to bet?: "))
  if howBigABet <= balance:
     pickNumber = int(input("Please PicK A Number: "))
     from random import randint
     value = randint(0, 1)
     print(value)
     if pickNumber == value:
           print(value)
           balance = balance + howBigABet * 2
           print("you won " + str(howBigABet * 2))
           print(balance)
           return balance


     else:
      print ("Sorry Wrong Number")
      balance = balance - howBigABet
      print("your new balance is: " + str(balance))
      return balance
  if balance <= 200:
   print("You Are Low on Cash Top up?")
  elif balance <= 0:
   print("You Are Out Of cash " + name + " Top Up ")
   return balance

 else:
    print("Sorry You Dont Have Enough Money")


print("Hello " + name)
passwordAttempted = input("Please Enter Your Password: ")


if passwordAttempted == password:
    print("Welcome " + name)
    balance = playgame()
    while input("You want to play again? Y/N").lower() == "y": 
        balance = playgame(balance)
else:
    print ("Wrong Password " + name + " !")

请注意,此代码未经测试,可能无法按照您的意愿工作。


推荐阅读