user3050565
user3050565

Reputation: 37

Python - basic issue with if statement?

why do I keep getting "r" in results instead of "0" when I type r or R in input?

from random import randint

# Input
print("Rock: R   Paper: P   Scissors: S")
x = input("Please pick your choice: ")
y = randint(0,2)

if x.lower() == "r":
    x == 0;


print("value entered ", x, "value generated ", y)

Upvotes: 0

Views: 62

Answers (2)

Stephane Rolland
Stephane Rolland

Reputation: 39926

x == 0; is a boolean evaluating to False in your case. It tests if the value of x is equal to 0.

What you want is x = 0;

Upvotes: 1

Max Noel
Max Noel

Reputation: 8910

There are two problems:

  • First, you want x = 0 (assignment), not x == 0 (comparison).
  • Second, your test (if x.lower() == "r":) is always going to be false, because your input has a trailing newline (that is, x will usually be something like "r\n". You want if x.strip().lower() == "r": instead.

Upvotes: 0

Related Questions