0

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)
2
  • 1
    x == 0; is a boolean evaluating to true, you want x = 0 Commented Nov 29, 2013 at 20:14
  • What is your python version? 3x or 2x? Commented Nov 29, 2013 at 20:27

2 Answers 2

1

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;

Sign up to request clarification or add additional context in comments.

2 Comments

I'm guessing it'd be false if x is 'r', not that it matters :)
I think OP knows about his statement if x.lower() == "r" but not about x == 0; meaning
0

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.

4 Comments

Thats why raw_input is better.
No, raw_input is better because input runs eval on the user-supplied input and is a huge security risk (try typing __import__('os').system('rm -rf ~') at an input prompt, for example). raw_input will give you the trailing newline just the same. However, Python 3's input is Python 2's raw_input, renamed (Python 2's input was removed in Python 3).
You're assuming he/she's using python 2.x
in python 2.x raw_input doesn't give the trailing \n. See the help(raw_input). It says Read a string from standard input. The trailing newline is stripped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.