-2

I asked a similar question before, except the solution did not seem to work. I am writing a dice rolling game that if any one of the player's numbers matches the computer's numbers, the player wins and a message prints "You win." If otherwise, the elif statement means the computer wins and it prints "You lose." My problem is that the elif statement won't print "you lose." it just keeps printing "you win."

import random

die1 = 0
die2 = 0
die3 = 0
roll1 = 0
roll2 = 0
roll3 = 0

def dice_roll():
    dieroll = random.randint(1, 6)*2
    return dieroll

for die in range(12):
    die1 = int(input(f'Choose a number between 2 and 12: '))
    die2 = int(input(f'Choose a number between 2 and 12: '))
    die3 = int(input(f'Choose a number between 2 and 12: '))
    roll1 = dice_roll()
    roll2 = dice_roll()
    roll3 = dice_roll()
    if die1 or die2 or die3 == roll1 or roll2 or roll3:
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Win! - Thanks for playing!')
    elif die1 or die2 or die3 != roll1 or roll2 or roll3:
        print(f'Roll # 1 was {roll1}')
        print(f'Roll # 2 was {roll2}')
        print(f'Roll # 3 was {roll3}')
        print(f'You Lose! - Thanks for playing!')
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – jasonharper Oct 21 '20 at 04:12
  • Are you trying to test whether any of die{1,2,3} matches any of roll{1,2,3}? – Jonathan Dayton Oct 21 '20 at 04:16
  • 2
    Due to operator precedence, the if condition is `bool(die1) or bool(die2) or (die3 == roll1) or bool(roll2) or bool(roll3)`. Since roll{1,2,3} are always positive, the condition will always evaluate to True. – Jonathan Dayton Oct 21 '20 at 04:19
  • Okay, so, if die1, 2, or 3 is equal to any (doesn't have to be all three) of roll1, 2, or 3 then the player wins. But if die1, 2, and 3 don't match any of roll1, 2, and 3 then the player loses. –  Oct 21 '20 at 04:20
  • `if {die1, die2, die3} & {roll1, roll2, roll3}:` – khelwood Oct 21 '20 at 04:23

4 Answers4

2

Inside of your for loop, the parameters of the if statement have been declared incorrectly. Here's an example to help clarify it:

a=1
b=3
if a or b == 2:
   print(True)
else:
   print(False)

The if statement in the above example will always print True because you are asking the following: "if a holds a value that is True/greater than 0 or if b is equal to 2: print True" In your case:

if die1 or die2 or die3 == roll1 or roll2 or roll3

You are declaring your parameters as "if die1, roll2, or roll3 have any True/greater than 0 values or if die3 is equal to roll1: ...", so just change this to the actual values you want them compared to as Abhigyan Jaiswal's answer says and it will work correctly.

Edwin
  • 31
  • 3
0

If you want to check if any of die1, die2, die3 matches any of roll1, roll2, roll3 then you can use:

print(f'Roll # 1 was {roll1}')
print(f'Roll # 2 was {roll2}')
print(f'Roll # 3 was {roll3}')
if {die1, die2, die3} & {roll1, roll2, roll3}:
    print('You win. Thanks for playing.')
else:
    print('You lose. Thanks for playing.')

This checks if the set of {die1, die2, die3} and the set of {roll1, roll2, roll3} have any elements in common.

Also, and by the way, random.randint(1, 6)*2 is not equivalent to rolling two dice. It's rolling one die and doubling the result; so all the odd numbers are excluded, and the probabilities are flattened. If you want to simulate rolling two dice, you want random.randint(1,6) + random.randint(1,6).

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • this makes so much more sense! Thank you! This actually helped a lot, and the code is functional and a lot neater than before! –  Oct 24 '20 at 14:05
0

Adding on to khelwood's answer, you can use this method if you prefer this syntax

From your code logic, it seems that the player automatically wins when they make at least 1 correct guess(which I am not sure if this is your intention).

if die1 == roll1 or die2 == roll2 or die3 == roll3:
    print(f'Roll # 1 was {roll1}')
    print(f'Roll # 2 was {roll2}')
    print(f'Roll # 3 was {roll3}')
    print(f'You Win! - Thanks for playing!')
else:
    print(f'Roll # 1 was {roll1}')
    print(f'Roll # 2 was {roll2}')
    print(f'Roll # 3 was {roll3}')
    print(f'You Lose! - Thanks for playing!')

Python evaluates each condition that is separated by the keyword. A non-null value would always return True

Hence if you are doing this method

elif die1 or die2 or die3 != roll1 or roll2 or roll3:

die1 die2 roll2 roll3 will always return True and that is what causing your program to always print "You lose"

Xinming
  • 23
  • 4
0
#You can do this as well
  
  if die1 == roll1 or die2 == roll2 or die3 == roll3:
    print(f'Roll # 1 was {roll1}')
    print(f'Roll # 2 was {roll2}')
    print(f'Roll # 3 was {roll3}')
    print(f'You Win! - Thanks for playing!')
  else:
    print(f'Roll # 1 was {roll1}')
    print(f'Roll # 2 was {roll2}')
    print(f'Roll # 3 was {roll3}')
    print(f'You Loose! - Thanks for playing!')