-1

i need x to be an integer so my next part of code works, but as soon as i remove quotation marks around 0,1 or 2 where it says "making input readable for computer" i get this error message.

from random import randint

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

y = randint(0,2)

#Making input readable for computer
if x.lower() == "r":
    x = 0;

if x.lower() == "p":
    x = "1";

if x.lower() == "s":
    x = "2";

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

if (x == y):
    print("It's a draw!")


# Calculating "Who wins?"
if x == 0 and y == 1:
    print("Computer wins!")
if x == 0 and y == 2:
    print("You won!")

if x == 1 and y == 0:
    print("You won!")
if x == 1 and y == 2:
    print("Computer wins!")

if x == 2 and y == 0:
    print("Computer wins!")
if x == 2 and y == 1:
    print("You won!")
MPelletier
  • 16,256
  • 15
  • 86
  • 137
user3050565
  • 37
  • 2
  • 2
  • 7
  • 2
    Why not use a different variable for the user generated integer instead of x? – squiguy Dec 04 '13 at 22:13
  • Python2.x or python3.x? I assume python3.x? – mgilson Dec 04 '13 at 22:16
  • 3
    Aren't you comparing apples and oranges (`int` and `str`) ? to be clear, `"1" != 1` – Giupo Dec 04 '13 at 22:16
  • It's python 3, otherwise you get a different error, because of the input() call, and not using raw_input() – jgritty Dec 04 '13 at 22:17
  • The user is entering a string. In python 3, input() does just what you want. In python 2 the input of input() is evaluated. http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – jgritty Dec 04 '13 at 22:27
  • `(1).lower()` generates this exception - *start* with the information provided by the exception message. Then it's just a matter of finding out *why* the variable evaluates to an integer, as opposed to a string as what is expected. – user2864740 Dec 04 '13 at 22:43

5 Answers5

3

You should be using elif here:

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

elif x.lower() == "p":
    x = 1

elif x.lower() == "s":
    x = 2

Otherwise, all three conditions are evaluated with every run. Meaning, if the first passes, then x will be an integer for the second.


Also, you should write your code like this:

x = x.lower()  # Put this up here

if x == "r":
    x = 0

elif x == "p":
    x = 1

elif x == "s":
    x = 2

That way, you don't call str.lower multiple times.

Lastly, Python does not use semicolons.

  • 3
    I think that you might want to make the latter `"1"` and `"2"` into ints, not strings ... – mgilson Dec 04 '13 at 22:18
  • @mgilson - Agreed. I originally was going to leave the OP's code as is...but I think that is what he wants. –  Dec 04 '13 at 22:34
2

You are calling x.lower() after you assign x to an integer.

Also, you should probably not use the same variable for the integer and the input string.

jgritty
  • 11,660
  • 3
  • 38
  • 60
0

iCodez answer is the one, but you should just use the strings, like the following, if you are not using the number conversion to factor your print statement, not both.

Edit: Had to change what y was, oops

x = raw_input("Please pick your choice: ").lower()
y = choice(['r','p','s'])

if (x == y):
    print("It's a draw!")

# Calculating "Who wins?"
if x == 'r' and y == 'p':
    print("Computer wins!")
elif x == 'r' and y == 's':
    print("You won!")

elif x == 'p' and y == 'r':
    print("You won!")
elif x == 'p' and y == 's':
    print("Computer wins!")

elif x == 's' and y == 'r':
    print("Computer wins!")
elif x == 's' and y == 'p':
    print("You won!")

Now if you want to go with the convertion to integer then you could just use this:

y = randint(0,2)

if x == "r":
    x = 0

elif x == "p":
    x = 1

elif x == "s":
    x = 2

print ['tie', 'you win', 'they win'][x-y]

And semicolons are not needed in Python, but you can still use them if it makes you comfortable.

Edit: Just for fun.

import random
pick = ['r', 'p', 's']
x = ""
while x not in pick:
    x = str(raw_input("r, p, or s?  ")).lower()
print ['tie', 'y win', 'y win', 'x win', 'x win'][ord(x)-ord(random.choice(pick))]
MakeCents
  • 742
  • 1
  • 5
  • 15
0

With a couple of dictionaries this code will be short and concise:

x_conversion = {'r':0, 'p':1, 's': 2}
x = x_conversion[x.lower()]

or list(in this particular case)

x_conversion=['r', 'p', 's]
x = x_conversion.index(x.lower())

And for winner

winner_choice = {(0,1): 'Computer', (1, 2): 'You', ...}
winner = winner_choice[(x, y)]

Don't forget try/except and you'll have your results in much shorter and more readable code

volcano
  • 3,578
  • 21
  • 28
-2

Use raw_input() instead of input.

Also this should be:

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

nitrobuster
  • 321
  • 3
  • 18
  • I just tested the code in python. I don't mean to argue, but it works correctly. – nitrobuster Dec 04 '13 at 22:53
  • In python 3 `input()` is the correct function to use. It replaces `raw_input()`. `x` should not be a string, it should be an integer, it is the other 2 assignments that are wrong, where `x = "1"` and `x = "2"`. Those should be `x = 1` and `x = 2` respectively, otherwise the later comparisons are apples to oranges, or strings to integers. – jgritty Dec 04 '13 at 23:11