Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Pitrov Secondary
Pitrov Secondary
5,121 Points

Why is it ignoring my if statements

I am working on a small program with the skills I've learned, but it just ignores my if statements and just keeps on going, why. Here's my code

#first ask for password, if password right then
#let them play a game or change password
#game 1 is like Rock paper siccors
#may have more games
import random

password = 'python'

def rock_paper_scissors(player_turn):
    random_number = random.randint(1, 3)
    if random_number == 1:
        random_number = 'rock'
    elif random_number == 2:
        random_number = 'paper'
    elif random_number == 3:
        random_number = 'scissors'

    player_turn = player_turn.upper()
    random_number = random_number.upper()
    winner = ''
    if random_number == player_turn:
        winner = 'None of us'

    if random_number == 'ROCK' and player_turn == 'PAPER':
        winner = 'Human'
    elif random_number == 'PAPER' and player_turn == 'SCISSORS':
        winner = 'Human'
    elif random_number == 'SCISSORS' and player_turn == 'ROCK':
        winner = 'Human'

    elif random_number == 'SCISSORS' and player_turn == 'PAPER':
        winner = 'Computer'
    elif random_number == 'ROCK' and player_turn == 'SCISSORS':
        winner = 'Computer'
    elif random_number == 'PAPER' and player_turn == 'ROCK':
        winner = 'Computer'
    print("I choose {} you schoose {}. {} won".format(random_number.lower(), player_turn.lower(), winner))



while True:
    while True:
        checking_password = input('Password: ')
        if checking_password == password:
            break
    print("Great job, You're in")
    print("What do you want to do now?")
    print('Perhaps a game, or do you want to change the password? type q or quit to quit')
    what_to_do = input('Type game for our games, or to change password type password ')

    if what_to_do.upper() == 'GAME' or 'GAMES':
        print("\n\nHere's my games, Rock Paper Scissors or some other game")
        what_game = input("Type the name of the game you want to play, or type the first letters, like RPC for rock paper scissors ")

        if what_game.upper() == 'ROCK PAPER SCISSORS' or 'RPS':
            turn = input('Rock Paper Scissors. (take a turn) ')
            while True:
                if turn.upper() == 'ROCK' or 'PAPER' or 'SCISSORS':
                    rock_paper_scissors(turn)
                    break
                else:
                    turn = input('Please type something valid ')

Which if statement?

3 Answers

It's not that you can't have an or it's that unlike english you have to tell it what you are comparing. Python reads that code like like "Is what_to_do.upper() == 'GAME' true?" or "Is 'GAME' true?" GAME would always be true.

You want:

if what_to_do.upper() == 'GAME' or what_to_do.upper() == 'GAMES':

or

if what_to_do.upper() in ['GAME', 'GAMES']:

or

if what_to_do.upper().startswith('GAME'):

Pitrov Secondary
Pitrov Secondary
5,121 Points

Myers Carpenter, The three last ones, they should check if I typed game or games, rock paper scissors or rps, rock or paper or scissors, but it does not care of what I type in, it just continues the code, I can't figure out why it's doing that.

Pitrov Secondary
Pitrov Secondary
5,121 Points

I figured out how I could fix this problem. I couldn't have or in the statement. Instead of

    if what_to_do.upper() == 'GAME' or 'GAMES':

I have to do:

    if what_to_do.upper() == 'GAMES':

But why is that? Why can't I have 'or' in my statement?