[英]how to stop this infinite loop
我正在尝试制作一个猜数字游戏,如果用户匹配幸运数字,他们将赢得现金,游戏将继续进行直到现金用完为止。 他们为每轮比赛付出赌注。 每回合都会生成一个新的随机数。
我只想使用一个输入。 当我将输入放入循环中时,循环将停止无限循环,当我将其移出循环之外时,它将开始无限循环。 如何停止呢? 我需要在while循环中添加什么? 我尝试过休息,但我想继续比赛直到钱花光了。
我只想使用一个输入,但是当我将输入移出循环(lucky_guess)时,它将永远持续下去。 我只希望用户猜测一次。
funds = 1000
stake = 1
luckyNumber = 25 #test number #random number from 1 - 30
while funds != 0:
funds = funds - 1
print("Your Funds", funds)
luckyGuess = int(input("enter a number")) #this is the problem if i take this out, the loop wont stop
if luckyNumber == luckyGuess:
print("Number is",luckyNumber, " You Win, +35 added to your funds")
funds = funds + 35
else:
print("Fail, Try Again")
我相信格式应该是这样的:
funds = 1000
stake = 1
luckyNumber = 25 #test number #random number from 1 - 30
while funds != 0:
funds = funds - 1
print("Your Funds", funds)
luckyGuess = int(input("enter a number"))
if luckyNumber == luckyGuess :
print("Number is",luckyNumber, " You Win, +35 added to your funds")
funds = funds + 35
else:
print("Fail, Try Again")
使用break
关键字退出循环:
funds = 1000
stake = 1
luckyNumber = 25 #test number #random number from 1 - 30
while funds != 0:
funds = funds - 1
print("Your Funds", funds)
luckyGuess = int(input("enter a number")) #this is the problem if i take this out, the loop wont stop
if luckyNumber == luckyGuess:
print("Number is",luckyNumber, " You Win, +35 added to your funds")
funds = funds + 35
break
else:
print("Fail, Try Again")
您可以在文档中找到有关控制流指令的更多信息
也许这有帮助
funds = 1000
stake = 1
luckyNumber = 25 #test number #random number from 1 - 30
while funds != 0:
funds = funds - 1
print("Your Funds", funds)
play = input("play more? (y/n)")
if( ( play == 'y') or (play == 'Y')): #condition to check for continuing gameplay
luckyGuess = int(input("enter a number"))
else:
break
if luckyNumber == luckyGuess :
print("Number is",luckyNumber, " You Win, +35 added to your funds")
funds = funds + 35
else:
print("Fail, Try Again")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.