python - 保存函数中的变量以在循环时重用
问题描述
我昨天才开始学习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 + " !")
解决方案
您可以将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 + " !")
请注意,此代码未经测试,可能无法按照您的意愿工作。
推荐阅读
- java - Android Kotlin / JAVA - 自 1970 年以来以秒为单位获取当前世界时间 / UTC
- twitter-bootstrap - 具有淡入淡出效果的 Bootstrap 4 弹出框?
- windows - IDE 崩溃后读取控制台应用程序的标准输出
- asp.net-core - 在 AspNetCore IApplicationBuilder.Map 中使用令牌/约束
- arrays - How to fix "dont get a clear Array after a Programm Call"
- android - Android 的 Unity 构建因 Gradle 错误而失败
- google-calendar-api - 通过 PHP 中的 API 将事件添加到 Google 日历
- apache-spark - 明文 JDBC 临时视图中的 Spark JDBC 密码
- macos - 在 macOS 上安装旧版本的 Kong
- excel - Excel VBA:获取谷歌视图源