-1
import random
userChoice = input('Pick "r", "p", "s": ')
computerChoice = random.choice(['r', 'p', 's'])

p1 = str(userChoice)
p2 = str(computerChoice)

print("P1: Choose: ", p1)
print("P2: Choose: ", p2)


if p1 == p2 :
    print("DRAW!")
elif p1 is 'r' and p2 is 's' or p1 is 'p' and p2 is 'r' or p1 is 's' and p2 is 'p':
    print("Player 1: Has won the match")
else:
    print("Player 2: Has won the match")

My IDE spews warning when I am using this code?

Currently learning python, I would appreciate an explanation if possible!

Thank you.

The code works as is but I get errors before I run it saying logical error.

Hyps
  • 1

2 Answers2

0

Instead of is use == as it is direct comparison.

elif p1 == 'r' and p2 == 's' or p1 == 'p' and p2 == 'r' or p1 == 's' and p2 == 'p':
    print("Player 1: Has won the match")
else:
    print("Player 2: Has won the match")
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
0

You should be using == instead of is to compare the characters:

elif p1 =='r' and p2 =='s' or p1 =='p' and p2 =='r' or p1 =='s' and p2 =='p':

since == compares if the value is the same, whereas is compares if they are pointing to the same object.

In your case, pyhton is tricky and tries to save memory for short string. but for longer strings comparing with is will not work