0

So i tried making a rock paper scissors game but some if statements are not working. code was written in python.

Is there something preventing the if statements ffrom running? or s there another problem

I tried a bunch of little changes but none of them work

code:

import random

moves = ('rock', 'paper', 'scissors')
while True:
    print("rock, paper, scissors. Go! ")
  
    userInput = input("Choose your move: ")
  
    botInput = random.choice(moves)

    if userInput == botInput:
        print(userInput + " VS " + botInput)
        print("DRAW")

    if userInput == "paper" and botInput == "rock":
        print(userInput + " VS " + botInput)
        print("Player Wins!")

    if userInput == "scissors" and botInput == "paper":
        print(userInput + " VS " + botInput)
        print("Player Wins!")
    if userInput == "rock" and botInput == "scissors":
        print(userInput + " VS " + botInput)
        print("Player Wins!")

    if userInput == "rock" and botInput == "paper":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")

    if userInput == "paper" and botInput == "scissors":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")

    if userInput == "scissors" and botInput == "rock":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")
    
    print("Wanna Rematch?")
    decision = input("Yes or No? ")
    if decision == "Yes":
        pass
    elif decision == "No":
        break
  
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Nov 05 '22 at 12:26
  • 2
    which if statements are not working, what have you put as input? – CrafterKolyan Nov 05 '22 at 12:28
  • 2
    "Not functioning properly" is too vague to be useful. Please describe *how* it isn't functioning properly. – John Coleman Nov 05 '22 at 12:29
  • 2
    Hints: What if the user types "ROCK", "Rock" or "banana" for their move? What happens if the user types "I don't know", "Y" or "nope" for their rematch decision? – Dennis Williamson Nov 05 '22 at 12:40

3 Answers3

1

Here's my solution to this. I cleaned up the amount of if-else statements in your code as well with a variable outcome_map. I also removed a lot of the typing rarities that come with inputs by introducing your choice to be "(1, 2, or 3)":

import random

moves = ('rock', 'paper', 'scissors')
while True:
    print("Rock, paper, scissors. Go! ")
    user_input = int(input("Choose your move (1:rock, 2:paper, 3:scissors): "))

    if user_input not in [1, 2, 3]:
        print("Invalid input. Try again...")
        continue
  
    bot_move = random.choice(moves)
    user_move = moves[user_input-1]

    outcome_map = { # item A vs item B -> Outcome for A
        "rock":{
            "rock":"Draw",
            "paper":"Lose",
            "scissors":"Win",
        },
        "paper":{
            "rock":"Win",
            "paper":"Draw",
            "scissors":"Lose",
        },
        "scissors":{
            "rock":"Lose",
            "paper":"Win",
            "scissors":"Draw",
        }
    }

    outcome = outcome_map[user_move][bot_move]
    print("Player: ", outcome, "!")

    
    decision = input("Wanna Rematch? (y/n) ")
    if decision in ["y", "yes", "Y", "YES"]:
        pass
    else:
        print("Exiting... ")
        break

Hope this helps!

0

I think there's nothing wrong with your code... however, it does not handle specific inputs, maybe that is your problem, here is my solution:

import random
moves = ('rock', 'paper', 'scissors')

while True:
    print("rock, paper, scissors. Go! ")

    userInput = input("Choose your move: ").lower()

    botInput = random.choice(moves)
    #check if input is valid
    if userInput not in moves:
        print("Choose a valid move! Wanna try again?")
        decision = input("Yes or No? ").lower()
        if decision == "yes" or decision == 'y': continue
            # skips the remaining body of the loop
        else: break

    elif userInput == botInput:
        print(userInput + " VS " + botInput)
        print("DRAW")

    elif userInput == "paper" and botInput == "rock":
        print(userInput + " VS " + botInput)
        print("Player Wins!")

    elif userInput == "scissors" and botInput == "paper":
        print(userInput + " VS " + botInput)
        print("Player Wins!")
    elif userInput == "rock" and botInput == "scissors":
        print(userInput + " VS " + botInput)
        print("Player Wins!")

    elif userInput == "rock" and botInput == "paper":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")

    elif userInput == "paper" and botInput == "scissors":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")

    elif userInput == "scissors" and botInput == "rock":
        print(userInput + " VS " + botInput)
        print("Bot Wins!")


    print("Wanna Rematch?")
    decision = input("Yes or No? ").lower()
    if decision == "yes" or decision == 'y': continue
    else: break
ugo_capeto
  • 129
  • 8
0

I hope you understand what I did here:


moves = ('rock', 'paper', 'scissors')
decision = "Nothing"

while decision != "No":
    decision = "Nothing"
    print("rock, paper, scissors. Go! ")
  
    userInput = input("Choose your move: ")
  
    botInput = random.choice(moves)

    if userInput == "paper":
        if botInput == "rock":
            print(userInput + " VS " + botInput)
            print("Player Wins!")
        elif botInput == "paper":
            print(userInput + " VS " + botInput)
            print("DRAW")
        elif botInput == "scissors":
            print(userInput + " VS " + botInput)
            print("Bot Wins!")
    else:
        print("Invalid choice.")

    if userInput == "scissors":
        if botInput == "rock":
            print(userInput + " VS " + botInput)
            print("Bot Wins!")
        elif botInput == "paper":    
            print(userInput + " VS " + botInput)
            print("Player Wins!")
        elif botInput == "scissors":
            print(userInput + " VS " + botInput)
            print("DRAW")

    if userInput == "rock":
        if botInput == "rock":
           print(userInput + " VS " + botInput)
           print("DRAW") 
        elif botInput == "paper":    
           print(userInput + " VS " + botInput)
           print("Bot Wins!")
        elif botInput == "scissors":
           print(userInput + " VS " + botInput)
           print("Player Wins!") 
    
    while decision  != "Yes" and decision != "No":
        print("Wanna Rematch?")
        decision = input("Yes or No? ")
        if decision == "Yes":
            pass
        elif decision == "No":
            break
        else:
            print("Invalid answer.")
Jack142
  • 43
  • 7