带有轻微错误的基本骰子游戏...当你滚动双打时告诉你胜负

时间:2015-02-11 06:00:54

标签: python

如问题所述,我为基础骰子游戏写了这个。你要么掷双打,要么将两个骰子加到7或11,你就赢了。奇怪的是,我写它的方式导致它说你赢了输,当你滚动一个加起来低于10的双倍。我知道它很傻但我几乎不理解python而且烧得自己这么远......

import random

while input:
    print ('Take a chance and roll the dice!')
    print ('(Press enter to roll!)')
    input ()
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    roll = int(dice1) + int(dice2)
    print ('You rolled a', dice1, 'and a', dice2, '. Your total is', dice1 + dice2)

    if dice1 == dice2:
        print ('Well done, you rolled a double, you win!')

    if roll == 7 or roll == 11:
        print('You win!')

    elif roll == 3 or roll == 4 or roll == 5 or roll == 6 or roll == 8 or roll == 9 or roll == 10:
        print ('Dang, you lose!')

2 个答案:

答案 0 :(得分:2)

它现在输入两个if语句。您需要将第二个if更改为elif

elif roll == 7 or roll == 11:
    print('You win!')

除此之外,如果您重构最后一个if语句,它看起来会更好:

elif roll in range(3, 11): # 7 is in the list but, doesn't matter
    print ('Dang, you lose!')

答案 1 :(得分:0)

而不是放

elif roll in range(3, 11): 
    print ('Dang, you lose!')

你可以尝试

if roll != 7 or roll != 11:
    print('Dang, you lose!')

对不起伙伴,我打算放那个。