Adam
Adam

Reputation: 1

Why isnt this simple "rock paper scissors" program working in Python?

I'm new to python and I'm trying to write a program in which the user plays rock paper scissors against the computer. However, I don't know why it isn't working. Here's the code:

import random

computerResult = random.randint(1,3)

print("Lets play Rock Paper Scissors")
playerResult = input("Type 1 for Rock, 2 for Paper or 3 for Scissors: ")

if computerResult == 1:
    print("Computer says Rock")
if computerResult == 2:
    print("Computer says Paper")
if computerResult == 3:
    print("Computer says Scissors")


if playerResult == computerResult:
    print("Its a draw")
elif playerResult == 1 and computerResult == 2:
    print("You lose")
elif playerResult == 1 and computerResult == 3:
    print("You win")
elif playerResult == 2 and computerResult == 1:
    print("You win")
elif playerResult == 2 and computerResult == 3:
    print("You lose")
elif playerResult == 3 and computerResult == 1:
    print("You lose")
elif playerResult == 3 and computerResult == 2:
    print("You win")

When I run the program and play the game this is what i get:

Lets play Rock Paper Scissors
Type 1 for Rock, 2 for Paper or 3 for Scissors: 1
Computer says Scissors

It doesn't say whether I won or lost and I cant figure out why

Upvotes: 0

Views: 245

Answers (4)

theBugger
theBugger

Reputation: 451

The problem will be fixed casting the user input to an integer.

I'll also add a check to handle the bad input casistic:

goodResult = False

playerResult = 0

while not goodResult:

    inp = input("Type 1 for Rock, 2 for Paper or 3 for Scissors: ")

    try:
        playerResult = int(inp)
        goodResult = (playerResult > 0 and playerResult < 4)

    except:
        goodResult = False

Upvotes: 0

Chris
Chris

Reputation: 22953

The problem with your code is is that your assuming the input() function returns a integer. It does not, it returns a string. Change this line

playerResult = input("Type 1 for Rock, 2 for Paper or 3 for Scissors: ")

to this:

playerResult = int(input("Type 1 for Rock, 2 for Paper or 3 for Scissors: "))

With the int() function you are converting your input to a integer.

Upvotes: 0

bravosierra99
bravosierra99

Reputation: 1371

This should fix it

playerResult = input("Type 1 for Rock, 2 for Paper or 3 for Scissors: ")
playerResult = int(playerResult)

Upvotes: 1

Dmitry Torba
Dmitry Torba

Reputation: 3194

Because playerResult is a str and computerResult is int. And comparing string to int always results in False

Change

playerResult = input("Type 1 for Rock, 2 for Paper or 3 for Scissors: ")

To

playerResult = int(input("Type 1 for Rock, 2 for Paper or 3 for Scissors: "))

Upvotes: 6

Related Questions