I am creating a game close to rock paper scissors game but it won't work

Viewed 45
import random

# 2 beats 1 and 3 beats 2 and 1 beats 3.

list =  ["1", "2", "3"]

your_choice = input("1, 2 or 3?\n").lower()

computer_choice = print(random.randint(1, 3))

if your_choice == computer_choice:
    print("you tied!")

# If the player choice is 2 and computer choice is also 2 it should print you tied but it don't.
4 Answers

First print doesn't return anything, so computer_choice is None.

To fix this, split that line into multiple:

computer_choice = random.randint(1, 3)
print(computer_choice)

Second, the lower call is unnecessary because you're expecting numbers as input. You probably should add something to ensure a valid number is given though.

Third, you're taking in a string from the user input, and comparing to an integer. To fix this, you need to case the string to an integer or vice versa before you compare them:

if int(your_choice) == computer_choice:
    print("you tied!")

Couple things,

  1. input is reading the value as a string, need to cast it to int type so your if statement can compare
  2. the print() function does not return a value. Need to set the computer_choice variable then print.
list = ["1", "2", "3"]

your_choice = int(input("1, 2 or 3?\n"))

computer_choice = random.randint(1, 3)
print(computer_choice)
if your_choice == computer_choice: print("you tied!")

Your issue is primarily with this code:

if your_choice == computer_choice:
    print("you tied!")

In the code you submitted, your_choice is a string because keyboard inputs are always strings. However, computer_choice is an integer. So, if you choose 2 and the computer chooses 2, Python will attempt to compare "2" to 2, which will return False, because the values have incompatible types.

To fix this, simply cast the input value to an integer:

your_choice = int(input("1, 2 or 3?\n").trim())

Note that I also trimmed your input to remove whitespace before casting it.

I assume that you're using the print when creating computer_choice for debugging purposes, but putting it in the assignment will result in it having a value of None. If you want to debug something using print, put the print on a separate line, like this:

computer_choice = random.randint(1, 3)
print(computer_choice)
import random

# 2 beats 1 and 3 beats 2 and 1 beats 3.

list =  ["1", "2", "3"]

your_choice = int(input("1, 2 or 3?\n"))

computer_choice = random.randint(1, 3)
print(computer_choice)

if int(your_choice) == computer_choice:
    print("you tied!")
Related