-2

I am learning python from scratch. I have written one small code for a rock paper scissor game. where 1 input is from the user and another input from PC using the random library. I know my code is not perfect. but I am not understanding one thing when I was trying to test my code.

Here is my code.

import random
from time import sleep
print("Let's play the game")

print("For Rock Choose 1")
print("For Paper Choose 2")
print("For Scissors Choose 3")


a = int(input("Enter the number "))

if a == 1:
    print("You Choose Rock")
elif a == 2:
    print("You Choose Paper")
elif a == 3:
    print("You Chosse Scissors")
else:
    print("Invalid Input")

print ("Now taking input from PC")
sleep (3)

b = random.randint(1,3)
if b == 1:
    print("PC has given Rock")
elif b == 2:
    print ("PC has given Paper")
elif b==3:
    print ("PC has given Scissors")

if a == b:
    print ("Match Tie")
elif a ==1 & b == 3:
    print ("You Win")
elif a == 2 & b == 1:
    print ("You Win")
elif a == 3 & b == 2:
    print ("You Win")
else:
    print ("You Loose")

You can clearly see i choose 2 and PC choose 1. then the output i got is "You Loose" how it can possible in condition i clearly written if a == 2 & b == 1 print ("You Win")

While testing this code (selected code of image) I found one thing and I got confused.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
Saransh Agarwal
  • 241
  • 4
  • 10
  • When you post text, please post text, not photographs of text. This is a site for programming, not [photography.se]. We want to read&analyze the text, not write an essay critiquing its use of color and perspective. – Jörg W Mittag Sep 25 '19 at 18:53
  • Possible duplicate of [Logical vs. bitwise operator AND](https://stackoverflow.com/questions/36310789/logical-vs-bitwise-operator-and) – Fred Larson Sep 25 '19 at 19:04

2 Answers2

2

You need to use and rather than &

In Python, the & is a bitwise operator whereas you want to use a conditional.

if a == b:
    print ("Match Tie")
elif a ==1 and b == 3:
    print ("You Win")
elif a == 2 and b == 1:
    print ("You Win")
elif a == 3 and b == 2:
    print ("You Win")
else:
    print ("You Loose")
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
2

You are using bit operators. The & with 1 and 2 you have:

1 = 00001
2 = 00010

1 & 2 = 0

You should use and instead of &

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31