所以我正在做类似轮盘赌的循环,效果很好,除了它有时会停在“#结束条件”部分(从底部向上10行),通常在2个循环之后停止。有什么了解输入()保持其价值或类似的东西,即使没有一个变种还是我做错了什么?
# -*-coding:utf-8 -*
import os, math, random
user_number = 50
cash = 0
balance = 0
go_on = True
while go_on:
# to_find init
to_find = random.randrange(50)
# bet and cash init
if cash == 0:
temp = input("How many cash do you bet ?\n")
if int(temp) > 0:
cash = int(temp)
else:
continue
temp = input("Which number do you bet on (0 to 49) ?\n")
if int(temp) >= 0 and int(temp) <= 49:
user_number = int(temp)
else:
continue
# cash modif
if user_number == to_find:
balance += cash * 3
cash *= 4
print("Woot we got a winner\nYou're now at,", cash, "$ !\nThe number was", to_find, "\n")
elif user_number % 2 == to_find % 2:
balance += math.ceil(cash / 2)
cash = cash + math.ceil(cash / 2)
print("Yay you got some money\nYou're now at", cash, "$ !\nThe number was", to_find, "\n")
else:
balance -= cash
cash = 0
print("Sometime you win... And sometime, you're back to 0 !\nThe number was", to_find, "\n")
# end condition
print("Your current balance is at", balance, "\n")
if input("Want to try again ? Hit enter ! \nElse enter a number\n") == "":
pass
else:
go_on = False
print("See you soon !")
if balance < 0:
print(" And better luck next time !")
os.system("pause")
答案 0 :(得分:2)
您有一个无限循环:如果在cash
不为0的情况下触及循环的底部,则在下一次迭代中,您将触及{{1}的else
部分}的条件,cash == 0
会带您回到循环的开始,然后循环重复一遍。
看看您要做什么,我最好的猜测是您假设continue
的工作方式像continue
(不执行任何操作,等同于省略pass
-部分)。
为了弄清楚这种情况是怎么回事,熟悉调试器可能是一项很好的投资,这将使您可以在任何行停止代码并调查每个本地代码的值。变量。周围有很多这样的东西,但是PyCharm Community就是一个很好的免费例子。