x  N  x x N x - 15 days ago 5
Python Question

python ignoring if statement

I've been working on a python program where the user plays rock, paper, scissors against the computer.

The user is prompted to enter 0 for paper, 1 for rock, and 2 for scissors. My program would print what each player chose and ideally I wanted it to also say something like "Paper covers rock. You win" but i can't seem to get my if statements to work and also I want my program to print "you chose paper" etc. but instead of the words rock, paper, or scissors it still says 0,1, and 2.

I tried assigning paper = 0. I thought maybe it wasn't working because the numbers weren't strings so I tried adding parentheses paper = "0", but that didn't work either. Also, when I created the variable 'player' I thought I correctly used the string function to turn whatever integer was entered into a string, but maybe there's something I'm not seeing.

Later I tried avoiding assigning paper, rock, and scissors to the integers and instead creating new variables. However, the new variables created within my if statements were declared as undefined so I must have messed it up somehow. I'm confused as to why it doesn't work because someone else wrote a program for the same purpose using if statements to create new variables and his program worked.

Another place I thought there might be an error is how i ended my if statements with elif instead of else so I changed that and got another an incorrect syntax error. I've been reading my textbook and searching the internet so I'm sorry if I've missed something that should be obvious. I'm super, super, super new to programming so my knowledge is very limited. This code has been warping so much today that i can't even remember how it was when it at least was able to finish compiling without running into an error.

As this code is now it runs into an error because player1 is not defined. I'm sorry that this program is such an eye-sore.

import random
player = input(str("Enter 0 for paper, 1 for rock, and 2 for scissors:" ))
computer = str(random.randint(0,2))
if player == 0:
player1 == "paper"
elif player == 1:
player1 == "rock"
elif player == 2:
player1 == "scissors"
elif computer == 0:
computer1 == "paper"
elif computer == 1:
computer1 == "rock"
elif computer == 2:
computer1 == "scissors"
print ("You chose " + player1 , "and the computer chose " , + computer1)
if player == "paper" and computer == "rock":
print("Paper covers rock. You win!")
elif player == "paper" and computer == "scissors":
print("Scissors cut paper. You lose!")
elif player == "paper" and computer == "paper":
print("You both chose paper. It's a draw!")
elif player == "rock" and computer == "paper":
print("Paper covers rock. You lose!")
elif player == "rock" and computer == "rock":
print("You both chose rock. It's a draw!")
elif player == "rock" and computer == "scissors":
print("Rock beats scissors. You win!")
elif player == "scissors" and computer == "paper":
print("Scissors cut paper. You win!")
elif player == "scissors" and computer == "rock":
print("Rock beats scissors. You lose!")
elif player == "scissors" and computer == "scissors":
print("You both chose scissors. It's a draw")


UPDATE: After a few errors have been brought to my attention I have fixed a few things and now my code looks like this:

import random

player = input("Enter 0 for paper, 1 for rock, and 2 for scissors:" )
computer = random.randint(0,2)

if player == 0:
player1 = "paper"
elif player == 1:
player1 = "rock"
elif player == 2:
player1 = "scissors"

if computer == 0:
computer1 = "paper"
elif computer == 1:
computer1 = "rock"
elif computer == 2:
computer1 = "scissors"

print ("You chose " + player1 , "and the computer chose " + computer1)

if player1 == "paper" and computer1 == "rock":
print("Paper covers rock. You win!")
elif player1 == "paper" and computer1 == "scissors":
print("Scissors cut paper. You lose!")
elif player1 == "paper" and computer1 == "paper":
print("You both chose paper. It's a draw!")
elif player1 == "rock" and computer1 == "paper":
print("Paper covers rock. You lose!")
elif player1 == "rock" and computer1 == "rock":
print("You both chose rock. It's a draw!")
elif player1 == "rock" and computer1 == "scissors":
print("Rock beats scissors. You win!")
elif player1 == "scissors" and computer1 == "paper":
print("Scissors cut paper. You win!")
elif player1 == "scissors" and computer1 == "rock":
print("Rock beats scissors. You lose!")
elif player1 == "scissors" and computer1 == "scissors":
print("You both chose scissors. It's a draw")


and I am now getting the error:

NameError: name 'player1' is not defined

Answer

In your updated version:

player = input("Enter 0 for paper, 1 for rock, and 2 for scissors:" )
computer = random.randint(0,2)
if player == "0":
   player1 = "paper"
elif player == "1":
  player1 = "rock"
elif player == "2":
  player1 = "scissors"
elif computer == "0":
   computer1 = "paper"
elif computer == "1":
   computer1 = "rock"
elif computer == "2":
   computer1 = "scissors"

There are (at least) two problems that cause computer1 to never get defined.

First, elif means "else if"—in other words, if player equals any of "0", "1", or "2", none of these computer tests are even going to happen.

Second, you've defined computer as an integer—either 0, 1, or 2. There's no way that a number can be equal to a string, so all of your comparisons are going to be false.

To fix these two problems—which will probably not be all of the problems in your code, just the two problems that lead to this NameError—you need this:

if player == "0":
   player1 = "paper"
elif player == "1":
  player1 = "rock"
elif player == "2":
  player1 = "scissors"
else:
  print "player is", player, "rather than a string for 0, 1, or 2!"

if computer == 0: # NOTE: not elif, and not "0"
   computer1 = "paper"
elif computer == 1:
   computer1 = "rock"
elif computer == 2:
   computer1 = "scissors"
else:
   print "computer is", computer, "instead of 0, 1, or 2!"

However, a much better way to do this is to use consistent types instead of randomly mixing numbers and strings, and to use a list or dict instead of a long chain of if statements. For example:

prs = ["paper", "rock", "scissors"]
player = int(input("Enter 0 for paper, 1 for rock, and 2 for scissors:"))
computer = random.randint(0, 2)
player1 = prs[player]
computer1 = prs[computer]

Now, besides avoiding a lot of repetition, you're also ensuring that any problems show up as soon as possible—if the user types spam or 76 you'll get an exception telling you that that 'spam' can't be turned into a number, or that 76 isn't a valid index, immediately, rather than getting a NameError about player1 or computer1 20 lines later.