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

or statement

Im not sure i understand how the or statement works.. Im basicly trying to tell the while loop that as long as first_or_second isnt "" or 1 or 2 keep going. But it dosent seem to work, same situation with the if first_or_second != 1 or 2. Can someone give me some insight to how the or statement works, and a fix for my code?

print("Welcome to a Tic Tac Toe game! You will play as X!")

first_or_second = ""

while first_or_second != "" or 1 or 2:
    try:
        first_or_second = int(input("Would you like to go first or second? 1/2 "))
    except ValueError:
        continue
    if first_or_second != 1 or 2:
        print("You can only input 1 or 2")
    else:
        print("Ok your playing {}".format(first_or_second))

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! There are a few things going on here. For an or statement to work, you need to say between each or exactly what you're looking at.

For example:

if x > 10 or x < -10:

You may not say:

if x > 10 or < -10:

This will result in a syntax error. You must write out the full expression to evaluate.

Now, let's get into the logic of an or statement especially dealing with when something is not equal to something else. For an or statement to be true, only one of them has to evaluate to true. So let's take a look at this:

if x != 1 or x != 2:

This will always evaluate to true. Why is that? Well, if we type in 1, it asks is 1 not equal to one? And yes, it's equal so that evaluates to false. It will then move into the next part. Is 1 not equal to 2? Yes, one is not equal to two and this part evaluates to true which makes the entire thing evaluate to true. This is how you were ending up in an infinite loop.

The operator you need here is the and operator. You want to make sure that what is there is neither 1 nor 2.

I took the liberty of reworking your code a bit so that it does what I think you want. Take a look:

print("Welcome to a Tic Tac Toe game! You will play as X!")

first_or_second = input("Would you like to go first or second? 1/2  ")

while first_or_second != 1 and first_or_second != 2:
    try:
        first_or_second = int(first_or_second)
    except ValueError:
        print("You can only input 1 or 2")
        first_or_second = input("Would you like to go first or second? 1/2  ")

print("Ok you're playing {}".format(first_or_second))

Note, I corrected the "your" to "you're" in the final line to make it grammatically correct. Hope this helps, but let me know if you have any questions! :sparkles:

Ty so much for taking the time to explain that! Makes sense now, and thx for the updated code!