Python Game of inbetween - can't get if-elif to work
Question: trying to make a game in Python.. although I can't seem to get a certain bit
of code working?! It's driving me nuts!

Any help is greatly appreciated!

    
    
    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!')
    

It's in the second if statement structure, and for whatever reason if die1 or
die2 are the same as die3, it just won't print 'You hit the post...' it just
ends!

Thanks!


Answer: 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](http://docs.python.org/2/reference/expressions.html#not-in) 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!')
    

