-1
    import random
    def gameres(comp, you):
        if comp == you:
            return None
# when comp = r
        elif comp == 'r':
            if you == 'p':
                return True
        elif comp == 'r':
            if you == 's':
                return False
# when comp = p
        elif comp == 'p':
            if you == 's':
                return True
        elif comp == 'p':
            if you == 'r':
                return False
# when comp = s
        elif comp == 's':
            if you == 'r':
                return True
        elif comp == 's':
            if you == 'p':
                return False
    
    print("Comp turn: Rock(r), Paper(p) and Scissor(s)")
    randno = random.randint(1, 3)
    if randno == 1:
        comp = 'r'
    elif randno == 2:
        comp = 'p'
    elif randno == 3:
        comp = 's'
    
    you = input("Your turn: Rock(r), Paper(p) and Scissor(s)")
    a = gameres(comp, you)
    
    print(f"Computer chose {comp}")
    print(f"You chose {you}")
    
    if a == None:
        print("The game is a tie!")
    elif a == True:
        print("You Win!")
    elif a == False:
        print("You Lose!")
    
   

I tried to create a rock paper and scissor game in Python but I'd seen a problem whenever I should lose, it returns "The game is a tie".

I think it is always returning None instead of False.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    If you have the same condition twice in an if/elif/else statement only the _first one_ actually gets entered. – jonrsharpe Jun 08 '22 at 06:41

2 Answers2

1

There is one flaw that is making it behave this way Like here if comp is 'r' and you is 's' so elif comp == 'r': is true so it will go inside First Part but then when you is not equal to 'p' it won't return anything and won't check for second elif case (second part).

        elif comp == 'r': # First part
            if you == 'p':
                return True
        elif comp == 'r': # Second Part
            if you == 's':
                return False

Try to do something like

        elif comp == 'r': # First part
            if you == 'p':
                return True
            elif you == 's':
                return False
Tinfinity
  • 84
  • 1
  • 8
0

As per jonrsharpe's comment, it makes no sense to have the same condition twice in an if/elif. Also, you don't need an elif at all if you have a return in the if block.

And finally, given a computer player choice you may simply check for the human player winning choice, and return the result of that check. So your function can be simplified like this:

    def gameres(comp, you):
        if comp == you:
            return None
# when comp = r
        if comp == 'r':
            return you == 'p':
# when comp = p
        if comp == 'p':
            return you == 's':
# when comp = s
        if comp == 's':
            return you == 'r':
gimix
  • 3,431
  • 2
  • 5
  • 21