我已经设置了一个RNG和一个continue按钮,但是在继续之后它不会更新结果

2024-09-28 22:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

#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点,如果我的大脑死了,我想不出什么办法来修复它


Tags: ortoloopyouifcashdicewin
2条回答

在每次游戏之前,用20重新初始化变量cash。要修复游戏,只需将代码移出循环即可

winloss的初始化也可以移出循环,因为它们不会改变

from random import randint语句相同,将所有import语句置于文件顶部是considered a good practice

from random import randint

#Setting up RNG
loop = "y"
win = 3
loss = 2
cash = 20
while loop == "y" or loop == "yes":
    dice = (randint(1,10))
    dice2 = (randint(1,10))
    roll = (dice + dice2)

    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()

我会重新排序您的代码,使控制流更清晰

为了比较“ifain多个元素”,应该使用set()s-当查找其中是否有内容(以及其他集合操作)时,它们非常有效

要打印,请查阅str.format()python 3.6+ string interpolation: PEP-498

如果只使用2个随机数的和,可以使用random.choices(iterable, k=2)一次性获得它们

from random import choices

cash = 20
winAmount = 3
lossAmount = 2

#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, else you loose $2

    You have a 20% chance of winning
""")
x = input("Press ENTER to start.")

lucky_numbers = {3,7,11,17}

# needed for outputting text
pluralize = {8,11,18}
win = False


loop = "y"
while loop and loop[0]== "y":
    sum_dice = sum(choices(range(1,11), k=2))

    if sum_dice in lucky_numbers:
        win = True
        cash += winAmount
    else:
        win = False
        cash -= lossAmount

    print("You {}. You rolled a{} {}!".format(
        "won" if win else "lost",
        "n" if sum_dice in pluralize else "", 
        sum_dice))

    print("")
    print("Cash - $" + str(cash))
    loop = input("Continue? (Y/N) ").lower().strip()

输出:

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, else you loose $2

    You have a 20% of winning

Press ENTER to start.
You lost. You rolled a 6!

Cash - $18
Continue? (Y/N) y
You lost. You rolled a 16!

Cash - $16
Continue? (Y/N) y
You lost. You rolled a 16!

Cash - $14
Continue? (Y/N) y
You lost. You rolled a 15!

Cash - $12
Continue? (Y/N) y
You won. You rolled a 7!

Cash - $15
Continue? (Y/N) n

除了打印格式化之外,输出还使用三元运算符(dox如果不是这样y)。更多信息:Does Python have a ternary conditional operator?

相关问题 更多 >