如何使用随机数字让我的骰子游戏停止给我错误

时间:2018-05-04 09:42:10

标签: python

它一直在突出显示的区域给我语法错误,请有人告诉我为什么我得到它们。 this image shows you the code that i have used and highlighted areas where i get a syntax error

如果链接不起作用,则代码中没有突出显示的区域:

goes = 3
while True:
    import random
    die1 = 0
    die2 = 0
    die3 = 0
    die1 = str(random.randint(1,6))
    die2 = str(random.randint(1,6))
    die3 = str(random.randint(1,6))

    if die1 == die2 == die3:
        print(die1+die2+die3)

    if die1 == die2 not die3:
        print(die1+die2-die3)

    if die1 == die3 not die2:
        print(die1+die3-die2)

    if die2 == die3 not die1:
        print(die2+die3-die1)

    else:
        print(0)
    goes=goes-1
    if goes < 1:
        break

    print ([die1],[die2],[die3])

1 个答案:

答案 0 :(得分:3)

die1 == die2 not die3并不意味着什么。我认为你的意思是:

die1 == die2 and not (die1 == die3)

或更简单:

die1 == die2 and die1 != die3

由于我们已经检查过这三者是否相等,我们可以使用elif来避免以后的比较:

if die1 == die2 == die3:
    print(die1+die2+die3)
elif die1 == die2:
    print(die1+die2-die3)
elif die1 == die3:
    print(die1+die3-die2)
elif die2 == die3:
    print(die2+die3-die1)

因此,如果代码达到elif die1 == die2:,则表示die1 == die2 == die3为假。