我正在尝试编写一个简单的Dice程序。我希望每次玩家获胜时增加sum变量和打印金额。提前谢谢
import random as rm
def gamble():
dice= [1,2,3,4,5,6]
x=input('what is the number ')
x=int(x)
secure_random = rm.SystemRandom()
y=int(secure_random.choice(dice))
print(y)
sum=0
while x==y :
sum=sum+50
print("You Won ",sum)
break
for i in range(20):
gamble()
答案 0 :(得分:1)
你需要突破你的循环。
while x == y:
print("You Won 50$")
break
也就是说,while
循环是一种不好的方法。
我会使用if
声明:
if x == y:
print("You Won 50$")
然后不需要break
。
此外,不要设置任何与print相同的值,并使值的类型相同。
curSum = 50
def gamble():
global curSum
dice = [1,2,3,4,5,6]
x = input('what is the number ')
x = int(x)
secure_random = rm.SystemRandom()
y = int(secure_random.choice(dice))
print(y)
if x == y :
# Change + 1 to whatever amount you want to add
curSum = curSum + 1
print("You Won %d $" % curSum)
for i in range(3):
gamble()