Home > OS >  Repeating "if" statement not considering "elif"
Repeating "if" statement not considering "elif"

Time:09-17

This is a simple game that I made, and I wanted to add the option to be able to play again and not have to run the program over and over. However, whenever I try to do that, it seems like it isn't considering or reaching the "elif" statement at the bottom of my code. How do I get it to work?

import random
from time import sleep as wait

# Definition of variables.
choices = [
    "rock",
    "paper",
    "scissors"
]
ai = random.choice(choices)
player = input("Enter rock, paper, or scissors: ").lower()

while True:
    for i in range(2147483647):
        # Makes sure the player types in something from the choices.
        if player not in choices:
            print("\nInvalid! Enter a valid choice. (Check your spelling!)")
            wait(0.5)
            player = input("\nEnter rock, paper, or scissors: ").lower()
        else:
            print(f"\nYou picked {player}.")
            break

    wait(1)

    print(f"\nai picked {ai}.\n")

    wait(1)
    # Every possible solution for the choices.
    if player == "rock" in choices and ai == "paper" in choices:
        print("The ai covered you in paper. (Lose)")
    elif player == "rock" in choices and ai == "scissors" in choices:
        print("You beat the ai's scissors to a plump. (Win)")
    elif player == "rock" in choices and ai == "rock" in choices:
        print("Y'all are beating each other with a rock, and no one wins. (Tie)")
    elif player == "paper" in choices and ai == "rock" in choices:
        print("You cover ai's rock in paper. (Win)")
    elif player == "paper" in choices and ai == "paper" in choices:
        print("Both of you try to cover each other in paper, endlessly. (Tie)")
    elif player == "paper" in choices and ai == "scissors" in choices:
        print("ai cuts you to pieces! (Lose)")
    elif player == "scissors" in choices and ai == "rock" in choices:
        print("ai beats your scissors to a plump with a rock. (Lose)")
    elif player == "scissors" in choices and ai == "paper" in choices:
        print("You cut ai to pieces! (Win)")
    elif player == "scissors" in choices and ai == "scissors" in choices:
        print("Y'all try to cut each other's metal somehow, endlessly. (Tie)")
    else:
        # This is in case the player found a bug.
        print("How did we get here?")

    wait(1)

    again = input("\nDo you want to play again? (yes/no): \n").lower

# Start of problem ---------------------------------------------------

    if again == "yes" or "ye" or "y":
        ai = random.choice(choices)
        player = input("Enter rock, paper, or scissors: ").lower()
        continue
    elif again == "no" or "n":
        break

# End of problem -----------------------------------------------------

CodePudding user response:

   again = input("\nDo you want to play again? (yes/no): \n").lower

This should be a typo, lower should be lower()

   if again == "yes" or "ye" or "y"

The condition might be improper that you might need to know what's operator precedence https://docs.python.org/3/reference/expressions.html#operator-precedence

I suggest you could use

  if again in ["yes","ye","y"]:

CodePudding user response:

if again == "yes" or "ye" or "y":

Should be like

if again == "yes" or again == "ye" or again =="y":

Otherwise it evaluates as true like this

if "ye":
  • Related