0
import random

def rps():
    for i in range(1,10):
        print("1 for R, 2 for P, 3 for S") 
        userchoice = input("Enter your number")
        a = random.randint(0,3)

        if userchoice == 1 and a == 3:
            print("Rock beats Scissors")
        elif userchoice == 1 and a == 2:
            print("Paper beats Rock")

        elif userchoice == 2 and a == 1:
            print("Paper beats rock")
        elif userchoice == 2 and a == 3:
            print("Scissors beats paper")

        elif userchoice == 3 and a == 1:
            print("Scissors beats paper")
        elif userchoice == 3 and a == 2:
            print("Scissors beat paper")
        elif userchoice == a:
            print("its a draw")


rps()

So im making a rock paper scissors game and need help. I think my problem is with the "and" statement. When i run the code and input my answer it doesnt do anything but ask the the question again (i used a while statement). If anyone can find the problem thanks in advance.

Yunkai Xiao
  • 191
  • 12
  • Your `userchoice` is a string, not a number. It is never equal 1, 2 or 3. – DYZ Feb 11 '18 at 23:14
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – OneCricketeer Feb 25 '18 at 15:38

2 Answers2

1

I think you're comparing a string against integer for the error you had.

try casting your input to int:

userchoice = int(input("Enter your number"))
Yunkai Xiao
  • 191
  • 12
0

another problem: the ranges of randint are inclusive, so the computer will be able to choose 0,

also, you could save a huge amount of time by presetting the conditions on an array, and then checking for a win or draw by iterating through it e.g.

user_wins = [[1,3],[2,1],[3,2]]
comp_wins = [[3,1],[1,2],[2,3]]

and then creating an array for all messages and using indices to decide which message to say.