#Setting up RNG
loop = "y"
while loop == "y" or loop == "yes":
from random import randint
dice = (randint(1,10))
dice2 = (randint(1,10))
roll = (dice + dice2)
win = 3
loss = 2
cash = 20
if roll == 3 or roll == 7 or roll == 11 or roll == 17:
cash += (win)
else:
cash -= (loss)
#Starting game
print("""Welcome to, Gambling for School!
You have $20 and must earn as much money as possible
If you roll a 3, 7, 11, or 17, you will win $3 but any other number
takes $2
You have a 20% of winning
""")
x = input("Press ENTER to start.")
#Results
if roll == 11 or roll == 8 or roll == 18:
print("You rolled an " + str(roll) + "!")
else:
print("You rolled a " + str(roll) + "!")
print("")
print("Cash - $" + str(cash))
loop = input("Continue? (Y/N) ").lower()
必须更改缩进以显示代码
当它运行时,我按回车键开始游戏,它正确地进行加减运算,但当我选择继续时,它就好像我从未输钱或赚过钱一样。现在是凌晨1点,如果我的大脑死了,我想不出什么办法来修复它
在每次游戏之前,用
20
重新初始化变量cash
。要修复游戏,只需将代码移出循环即可win
和loss
的初始化也可以移出循环,因为它们不会改变与
from random import randint
语句相同,将所有import语句置于文件顶部是considered a good practice我会重新排序您的代码,使控制流更清晰
为了比较“ifa
in
多个元素”,应该使用set()s-当查找其中是否有内容(以及其他集合操作)时,它们非常有效要打印,请查阅str.format()或python 3.6+ string interpolation: PEP-498
如果只使用2个随机数的和,可以使用random.choices(iterable, k=2)一次性获得它们
输出:
除了打印格式化之外,输出还使用三元运算符(dox如果不是这样y)。更多信息:Does Python have a ternary conditional operator?
相关问题 更多 >
编程相关推荐