1
import random

print("=====Rock Paper Scissors: The Python Game=====")
print("\n")

name = input("Player please enter you name:\n")
print("\n")


myItem = input("Enter Either Rock Paper or Scissors:\n")

if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors":
    print("Are you Ok!! I said enter rock paper or scissors")
    

#Computer's Choices
computerItem = random.randint(1,3)
if computerItem == 1:
    computerItem = "Rock"
    print(f"Computer chooses {computerItem}...")
if computerItem == 2:
    computerItem = "Paper"
    print(f"Computer chooses {computerItem}...")
if computerItem == 3:
    computerItem = "Scissors"
    print(f"Computer chooses {computerItem}...")


if myItem == computerItem:
    print("Try Again!! You and the CPU picked the same thing")

#How I can loose to the CPU
if myItem == "Rock" and computerItem == "Paper":
    print("[Paper covers Rock]")
    print("CPU WINS!!!")
if myItem == "Paper" and computerItem == "Rock":
    print("[Paper covers Rock]")
    print(f"{name} WINS!!!")

if myItem == "Scissors" and computerItem == "Rock":
    print("[Rock Smashes Scissors]")
    print("CPU WINS!!!")
if myItem == "Rock" and computerItem == "Scissors":
    print("[Rock smashes Scissors]")
    print(f"{name} WINS!!!")

if myItem == "Paper" and computerItem == "Scissors":
    print("[Scissors cut Paper]")
    print("CPU WINS!!!")
if myItem == "Scissors" and computerItem == "Paper":
    print("[Scissors cut Paper]")
    print(f"{name} WINS!!!")

I have been trying to make this rock paper scissors game loop all day. But each time I put the code in a while loop it keeps giving errors.

So how do I loop through the game without any issues?

PS - I just started learning python

  • 2
    what errors? provide the complete error traceback (starting from word "Traceback") that you get ([edit] your question and add it there) – Matiiss Dec 30 '21 at 01:27
  • 1
    `if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors":` This will always fail. More pythonic to use: `if myItem not in ("Rock", "Paper", "Scissors")` Even better to convert all to lower case first. – 001 Dec 30 '21 at 01:29
  • you can look at [this](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for the part about asking the user for a valid input – Copperfield Dec 30 '21 at 01:39
  • 1
    for the computer item you can use [choice](https://docs.python.org/3/library/random.html#random.choice) instead, ex: `random.choice(["Rock", "Paper", "Scissors"])` – Copperfield Dec 30 '21 at 01:46
  • Another problem I have is that when I run the code in vs code's terminal it works but when I use cmd it keeps showing an error saying random isn't defined – IKUBIE NEMIEBOKA Dec 30 '21 at 14:52

1 Answers1

1

I have tested your code and it works well, as for the looping, all I did was insert the whole program code into an infinite while loop while True, which is a loop that never stops until it's told to, then after each turn, I ask the user if he wants to play again or not, if he doesn't want to play again, the program breaks out of the infinite while loop and exits.

Find the code below:

import random

while True:
    print("=====Rock Paper Scissors: The Python Game=====")
    print("\n")

    name = input("Player please enter you name:\n")
    print("\n")


    myItem = input("Enter Either Rock Paper or Scissors:\n")

    if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors":
        print("Are you Ok!! I said enter rock paper or scissors")
        

    #Computer's Choices
    computerItem = random.randint(1,3)
    if computerItem == 1:
        computerItem = "Rock"
        print(f"Computer chooses {computerItem}...")
    if computerItem == 2:
        computerItem = "Paper"
        print(f"Computer chooses {computerItem}...")
    if computerItem == 3:
        computerItem = "Scissors"
        print(f"Computer chooses {computerItem}...")


    if myItem == computerItem:
        print("Try Again!! You and the CPU picked the same thing")

    #How I can loose to the CPU
    if myItem == "Rock" and computerItem == "Paper":
        print("[Paper covers Rock]")
        print("CPU WINS!!!")
    if myItem == "Paper" and computerItem == "Rock":
        print("[Paper covers Rock]")
        print(f"{name} WINS!!!")

    if myItem == "Scissors" and computerItem == "Rock":
        print("[Rock Smashes Scissors]")
        print("CPU WINS!!!")
    if myItem == "Rock" and computerItem == "Scissors":
        print("[Rock smashes Scissors]")
        print(f"{name} WINS!!!")

    if myItem == "Paper" and computerItem == "Scissors":
        print("[Scissors cut Paper]")
        print("CPU WINS!!!")
    if myItem == "Scissors" and computerItem == "Paper":
        print("[Scissors cut Paper]")
        print(f"{name} WINS!!!")
    exit = input("\nWould you like to play game? Y/N")
    if exit=="N":
        break

If you have any more questions, I'd love to answer them, good luck!

hunter
  • 36
  • 6