Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

not sure whats wrong with my code

+5 votes
asked Dec 31, 2022 by Isabella (240 points)
edited Dec 31, 2022 by Isabella
# define the three symbols
symbols = { "rock": 0, "paper": 1, "scissor":2}
# while we need to play, the game will play
# create a while loop
while True:
    break
    # generate a random number from 0-2
    # import the random module
    import random
    num = random.randint(0, 2)
    # get the input from the user
    user_input = int(input ("Enter your move (A, B, C"))
    user_choice = symbols[user_input]
    # Compare the choices, and print the output
# find the winner
    if symbols[num] == user_input:
        print("you win!")
    elif user_input == symbols[2-num]:
        print ("you lose!")
else:
            print (f"its a draw!")
# ask the user if they want to play again, by asking the user to enter "Y" or "N"
play_again = input("Would you like to play again? (Y/N")

1 Answer

0 votes
answered Dec 31, 2022 by Peter Minarik (86,040 points)
edited Dec 31, 2022 by Peter Minarik

Your Code

Find issues highlighted in your code below.
# define the three symbols
symbols = { "rock": 0, "paper": 1, "scissor":2}
# while we need to play, the game will play
# create a while loop
while True:
    break # you immediately quit the loop. Apply break only if play_again was Y and move that check inside the loop
    # generate a random number from 0-2
    # import the random module
    import random # usually imports are at the start of the program
    num = random.randint(0, 2)
    # get the input from the user
    user_input = int(input ("Enter your move (A, B, C")) # you cannot turn letters into numbers. Why not ask the user to enter numbers instead: [0, 1, 2]
    user_choice = symbols[user_input]
    # Compare the choices, and print the output
# find the winner
    if symbols[num] == user_input: # That's not how rock/paper/scissor works. Rock beats scissor, paper beats rock, rock beats scissors. That's not what you're doing here.
        print("you win!")
    elif user_input == symbols[2-num]:
        print ("you lose!")
else: # wrong indentation, you should have level 1 indentation here (not level 0)
            print (f"its a draw!") # wrong indentation, you should have level 2 indentation here (not level 1)
# ask the user if they want to play again, by asking the user to enter "Y" or "N"
play_again = input("Would you like to play again? (Y/N") # you should check this inside the loop, not outside

An alternative solution

This is how I would do it (without too much input checking and utilizing modulo 3 arithmetics):

import random
choices = ["rock", "paper", "scissors"]
playAgain = "Y"
while playAgain == "Y":
    userChoice = int(input("\nRock(0)/Paper(1)/Scissors(2)? "))
    computerChoice = random.randint(0, 2)
    print(f"You chose: {choices[userChoice]}")
    print(f"Computer chose: {choices[computerChoice]}")
    if userChoice == computerChoice:
        print("It's a tie!")
    elif (userChoice - computerChoice) % 3 == 1:
        print("You win!")
    else:
        print("You lose!")
    play_again = input("Would you like to play again (Y/N)? ").upper()
print("Thank you for playing. Bye-bye!")
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...