0

试图用 Python 制作游戏.. 虽然我似乎无法让某些代码工作?!快把我逼疯了!

任何帮助是极大的赞赏!

import random

die1 = random.randint(1,10)
die2 = random.randint(1,10)
die3 = random.randint(1,10)


if die1 > die2:
    print ('Die 1:', die2,'  Die 2:', die1,)
else:
    print ('Die 1:', die1,'  Die 2:', die2,)

    if die1 == die2 or die2 == die1:
        print('\nEven-steven!')
    else:
        print('\nNot the same, let\'s play!')

print ('\nDie 3:', die3,)



if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2: 
    print ('\n*** You win! ***')
elif die1 > die2:
    if die3 < die2 or die3 > die1:
        print('\n*** Sorry - You lose! ***')
elif die2 > die1:
    if die3 < die1 or die3 > die2:
        print('\n*** Sorry - You lose! ***')
**elif die3 == die1 or die3 == die2:
    print ('\n*** You hit the post - You lose double your bet! ***')**


print ('\nThanks for playing!')

它在第二个 if 语句结构中,无论出于何种原因,如果 die1 或 die2 与 die3 相同,它就不会打印 'You hit the post...' 它就结束了!

谢谢!

4

5 回答 5

1

Your diagnosis is incorrect. The last statement will match if die3 is equal to either die1 or die2:

>>> die1, die2 = 1, 2
>>> die3 = die1
>>> die3 == die1 or die3 == die2
True
>>> die3 = die2
>>> die3 == die1 or die3 == die2
True

However, if die1 is greater than die2 your first elif matches, but your comparison to die3 is flawed:

>>> die1, die2 = 2, 1
>>> die3 = die1
>>> die3 < die2 or die3 > die1
False

and nothing is ever printed. You need to clean up the logic for that case.

Since you only have 3 different outcomes; winning, losing and hitting the post, you can really simplify the whole thing down to:

if die1 < die3 < die2:
    print('\n*** You win! ***')
elif die3 == die1 or die3 == die2:
    print('\n*** You hit the post - You lose double your bet! ***')
else:
    print('\n*** Sorry - You lose! ***')

Note that I used chained comparisons to simplify the first if expression some more. You also made sure that die1 is lower than die2, so testing for die2 < die3 < die1 is always going to be False.

Another note: == should be transitive, so die1 == die2 or die2 == die1 is redundant. You can simplify that to just die1 == die2.

Simplifying your whole program:

import random

die1 = random.randint(1,10)
die2 = random.randint(1,10)

if die1 > die2:
    die1, die2 = die2, die1

print ('Die 1:', die1,'  Die 2:', die2,)

if die1 == die2:
    print('Even-steven!')
else:
    print("Not the same, let's play!")
    die3 = random.randint(1,10)

    print ('Die 3:', die3)

    if die1 < die3 < die2:
        print('\n*** You win! ***')
    elif die3 == die1 or die3 == die2:
        print('\n*** You hit the post - You lose double your bet! ***')
    else:
        print('\n*** Sorry - You lose! ***')

print ('Thanks for playing!')
于 2013-04-16T14:37:04.083 回答
1

不要让它让你发疯,保持冷静,并使用pdb.

if die3 in (die1, die2):
    import pdb; pdb.set_trace()

if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2: 
    print ('\n*** You win! ***')
elif die1 > die2:
    if die3 < die2 or die3 > die1:
        print('\n*** Sorry - You lose! ***')
elif die2 > die1:
    if die3 < die1 or die3 > die2:
        print('\n*** Sorry - You lose! ***')
**elif die3 == die1 or die3 == die2:
    print ('\n*** You hit the post - You lose double your bet! ***')**


print ('\nThanks for playing!')

现在运行它,当它中断时,开始调试(使用nfor next,检查每个条件的值等)。

于 2013-04-16T14:41:23.180 回答
1

通过简化代码,调试起来会容易得多。只有 3 个条件 winlose或者lose (double)不需要 6 个(嵌套)条件。也可以通过切换die1以及die2die1 > die2的程序输出和变量是否保持有意义。这是更理智的:

import random

die1 = random.randint(1,10)
die2 = random.randint(1,10)

if die1 > die2:
    die1, die2 = die2, die1

print('Die 1:',die1,'Die 2:',die2)

if die1 == die2:
    print('Even-steven!')
else:
    print("Not the same, let's play!")  

    die3 = random.randint(1,10) 
    print('Die 3:',die3)

    if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2: 
        print ('You win!')
    elif die3 == die1 or die3 == die2:
        print ('You hit the post - You lose double your bet!')
    else:
        print('Sorry - You lose!')

print('Thanks for playing!')
于 2013-04-16T14:42:44.270 回答
0

这个怎么样?

if die3 == die1 or die3 == die2:
    print ('\n*** You hit the post - You lose double your bet! ***')
else:
    if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2: 
        print ('\n*** You win! ***')
    elif die1 > die2:
        if die3 < die2 or die3 > die1:
            print('\n*** Sorry - You lose! ***')
    elif die2 > die1:
        if die3 < die1 or die3 > die2:
            print('\n*** Sorry - You lose! ***')
于 2013-04-16T17:26:55.243 回答
0

最后的 if 语句仅作为第三个条件的内部语句elif的替代解决方案的一部分一起处理。if

以下应该可以解决您的问题。注意变化

if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2: 
    print ('\n*** You win! ***')
elif die1 > die2 and (die3 < die2 or die3 > die1):
    print('\n*** Sorry - You lose! ***')
elif die2 > die1 and (die3 < die1 or die3 > die2):
    print('\n*** Sorry - You lose! ***')
elif (die3 == die1) or (die3 == die2):
    print ('\n*** You hit the post - You lose double your bet! ***')

享受。:)

于 2013-04-16T14:45:44.727 回答