-1

I do not know why the program i wrote does not work and i really want to know.

I've looked at many codes that work, but i still want to know why the code that I wrote does not work

user1_input = input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")
user2_input = input(" User 2 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")

def compare(user1_input,user2_input):
    if user1_input == user2_input:
        return("It is a tie!")

    elif user1_input == 1:
        if user2_input == 2:
            return("User 1 Wins")
        else:
            return("User 1 lose")

    elif user1_input == 2:
        if user2_input == 1:
            return("User 1 loses")
        else:
            return("User 1 wins")

    elif user1_input == 3:
        if user2_input == 1:
            return("User 1 wins")
        else:
            return("User 1 loses")

    else:
        return("Invalid input!")


print(compare(user1_input,user2_input))

if you put in 3 for user 1 and 1 for user 2, it is supposed to say User 1 loses, but it keeps saying invalid input.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Godseph
  • 31
  • 7
  • 3
    You need to convert the inputs to `int()`. `user1_input = int(input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:"))`. – roganjosh May 11 '19 at 12:01

3 Answers3

1

Because your input is a string, and you compare a string with a integer, and that is why it returns a invalid input:

You can cast string to int with int() or just compare it with a string as I did in the code bellow, the choice is yours.

    user1_input = input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")
    user2_input = input(" User 2 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")

    def compare(user1_input,user2_input):
        if user1_input == user2_input:
            return("It is a tie!")

        elif user1_input == '1':
            if user2_input == '2':
                return("User 1 Wins")
            else:
                return("User 1 lose")

        elif user1_input == '2':
            if user2_input == '1':
                return("User 1 loses")
            else:
                return("User 1 wins")

        elif user1_input == '3':
            if user2_input == '1':
                return("User 1 wins")
            else:
                return("User 1 loses")

        else:
            return("Invalid input!")


    print(compare(user1_input,user2_input))
Stefan
  • 375
  • 1
  • 9
  • 24
0

The problem is that user1_input and user2_input are strings and you compare them to int and therefore you False and Invalid input!.

To solve it do -

user1_input = int(input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:"))
user2_input = int(input(" User 2 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:"))
Tom Ron
  • 5,906
  • 3
  • 22
  • 38
0

input() returns a string value. You will need to convert that to integers for the comparisons to work.

So instead of:

user1_input = input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")
user2_input = input(" User 2 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:")

Try this instead:

user1_input = int(input(" User 1 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:"))
user2_input = int(input(" User 2 choose your pick: Rock = 1, Scissor = 2, paper = 3 \n:"))
Jizhou Yang
  • 138
  • 5