Search code examples
pythonuser-input

Rock, Paper, Scissors. Problem to tie. (python)


I followed a guide to make a rock paper scissors game. It was all going smoothly but the tutorial didn't include a way to tie the game. I thought I could do this myself but the code i wrote isn't working. I am very new to coding so I can't realise what I did wrong.

I thought this code would work but instead it's just print the text for when the player loses against the computer. Here is the code.

import random

uw = 0
cpw = 0

options = ["rock", "paper", "scissors"]

while True:
    uinput = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
    if uinput == "q":
        break

    if uinput not in options:
        continue

    rn = random.randint(0, 2)
    #rock: 0, paper: 1, scissors: 2
    cpp = options[rn]
    print("Computer picked", cpp + ".")

    if uinput == "rock" and cpp == "scissors":
        print("You won!")
        uw += 1

    elif uinput == "paper" and cpp == "rock":
        print("You won!")`
        uw += 1

    elif uinput == "scissors" and cpp == "paper":
        print("You won!")
        uw += 1
    elif uinput "paper" and cpp == "paper":
        print("It's a tie, no points.")
    
    elif uinput "rock" and cpp == "rock":
        print("It's a tie, no points.")
    
    elif uinput "scissors" and cpp == "scissors":
        print("It's a tie, no points.")
    
    else:
        print("You lost!")
        cpw += 1


print("You won", uw, "times.")
print("The computer won", cpw, "times")
print("Goodbye")

Solution

  • You're not comparing your tie rows to the string:

        elif uinput "paper" and cpp == "paper":
            print("It's a tie, no points.")
    

    Should be:

    elif uinput == "paper" and cpp == "paper":
        print("It's a tie, no points.")
    

    Here the == is missing from your code and that's what is checking that your uinput is the same as "paper"

    But, instead of repeating these elif statements with and for each of the options, the DRY (do not repeat yourself) best-practice of coding would insist you use:

    elif uinput == cpp:
        print("It's a tie, no points.")
    

    This compares uinput to the computer's selection directly so it doesn't matter which choice you both made, if they are the same, it's a tie.