0

I am making a rock, paper scissors game form application and the code runs. However i can't seem to get it to work properly. The user will type either paper, scissors or rock, then the computer would choose one of those three as well from the options list. It sometimes says you won, however you really lost. What is wrong with this code?

from sys import exit
from time import sleep
from random import choice

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

print("rock")
sleep(0.5)
print("paper")
sleep(0.5)
print("scissors")
sleep(0.5)
user_move = input("shoot: ")

cpu_move = choice(options)
print(cpu_move)

if cpu_move == "rock":
    if user_move == "rock" or "r":
        print("Tie!")
        exit(0)
    elif user_move == "paper" or "p":
        print("You Lose!")
        exit(0)
    elif user_move == "scissors" or "s":
        print("You Win!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "paper":
    if user_move == "rock" or "r":
        print("You Win!")
        exit(0)
    elif user_move == "paper" or "p":
        print("Tie!")
        exit(0)
    elif user_move == "scissors" or "s":
        print("You Lose!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "scissors":
    if user_move == "rock" or "r":
        print("You Lose!")
        exit(0)
    elif user_move == "paper" or "p":
        print("You Win!")
        exit(0)
    elif user_move == "scissors" or "p":
        print("Tie!")
        exit(0)
    else:
        print("error")
        exit(1)

else:
    print("error")
    exit(1)
tanmay_garg
  • 377
  • 1
  • 13

2 Answers2

0

The way you are checking user_move is incorrect. Also, you will need to reverse what you are printing everywhere because currently, it is being done from the computer's point of view.

This can be done and fixed as below:

from sys import exit
from time import sleep
from random import choice

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

print("rock")
sleep(0.5)
print("paper")
sleep(0.5)
print("scissors")
sleep(0.5)
user_move = input("shoot: ")

cpu_move = choice(options)
print(cpu_move)

if cpu_move == "rock":
    if user_move in ["rock", "r"]:
        print("Tie!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("You Win!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("You Lose!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "paper":
    if user_move in ["rock", "r"]:
        print("You Lose!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("Tie!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("You Win!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "scissors":
    if user_move in ["rock", "r"]:
        print("You Win!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("You Lose!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("Tie!")
        exit(0)
    else:
        print("error")
        exit(1)

else:
    print("error")
    exit(1)

I had also made the same game in python some time back. Check it out.

tanmay_garg
  • 377
  • 1
  • 13
  • @Ayush Gupta please mark the answer as accepted if it solves your problem or let me know if you are still having any issues. – tanmay_garg May 22 '20 at 04:56
-1

Ase mentioned by @SimonR, you have your comparisons backwards. You print the CPU's move, and compare print "win/tie/lose" as if the player was cpu_move.

One way to help avoid these logic errors is to simplify your code. Even if you fixed it one by one you would have many individual chances to fail - see all the related issues for alternative ways to structure this.

Cireo
  • 4,197
  • 1
  • 19
  • 24