0

first post, sorry if it doesn't fit the criteria or if there are similar questions. I googled before posting here and was not able to find anything about this. I'm learning python and created a very basic rock, paper, scissors game. I wanted the script to ask the user if he wants to replay the game at the end and I looked up how to do so, as I was stumped.

The script below, more specifically the while loop completely skips the elif statement and regardless if I type Y or y, it just says "ciaox" and ends. If it do it the other way around, it skips the elif statement and just restarts the game anyway.

Help would be appreciated.

import random

def game():

    sl = ["rock", "paper", "scissors"]
    us = input("Rock, paper, scissors?: ")
    sl.remove(us)
    pcs = random.choice(sl)

    if us == "rock" and pcs == "paper":
        print("You chose " + us + ". PC chose " + pcs + ". You lose.")
    elif us == "rock" and pcs == "scissors":
        print("You chose " + us + ". PC chose " + pcs + ". You win.")
    if us == "paper" and pcs == "rock":
       print("You chose " + us + ". PC chose " + pcs + ". You win.")
    elif us == "paper" and pcs == "scissors":
        print("You chose " + us + ". PC chose " + pcs + ". You lose.")
    if us == "scissors" and pcs == "rock":
        print("You chose " + us + ". PC chose " + pcs + ". You lose.")
    elif us == "scissors" and pcs == "paper":
        print("You chose " + us + ". PC chose " + pcs + ". You win.")

while True:
    game()
    replay = input("Would you like to play again? Y/N ")
    if replay == "N" or "n":
        print ("ciaox")
        break
    elif replay == "Y" or "y":
        game()
octavean
  • 1
  • 2
  • 1
    `or "n"` will cause it to always evaluate to true. – fredrik Oct 11 '21 at 12:03
  • change to `if replay == "N" or replay == "n":` and `elif replay == "Y" or replay == "y":` – chitown88 Oct 11 '21 at 12:04
  • Hi, thanks for the quick reply. Can you elaborate, please? I am a newbie and would like to learn as much as I can. – octavean Oct 11 '21 at 12:04
  • That evaluates similar to `if (replay == 'N') or ('n'):` where the latter will always be `True` since any non-empty string is truthy. You want `if replay in 'Nn':` or `if replay.lower() == 'n'` or something like that – Cory Kramer Oct 11 '21 at 12:05
  • change to `replay = input("Would you like to play again? Y/N ").upper()`. Then you can remove the `or "n"` and the `or "y"` from the conditional statements – chitown88 Oct 11 '21 at 12:07
  • Python won't understand that you mean if replay is equal to N or n. You have to tell it explicitly each time like this `if replay == "N" or replay == "n"` – Sandsten Oct 11 '21 at 12:09
  • Appreciate it a lot, guys. Thank you, honestly. Much love. – octavean Oct 11 '21 at 12:26

0 Answers0