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

Harris Handoko
Harris Handoko
3,932 Points

Using OR in while loop giving undesirable outcome

I was using the while loop to ask for the name of each team player to be added to the team list. I also set the conditions to either prompt.lower() == 'yes' or 'y', I figured if the user entered anything other than those two, it should break the loop and print the last statement. But as you can see, I entered "no" and it still entered the loop.

But if I erased the or condition, it works just fine. Am I missing something here?

team = []

prompt = input("Would you like to add a player to the list? (yes/no) ") while prompt.lower() == 'yes' or 'y': playerName = input("Enter the name of the player to add to the team: ") team.append(playerName) prompt = input("Would you like to add another player? (yes/no) ")

print("There are {} players on the team".format(len(team)))

===============================================================

console

treehouse:~/workspace$ python team.py
Would you like to add a player to the list? (yes/no) n
Enter the name of the player to add to the team: ^CTraceback (most recent call last):

Harris Handoko
Harris Handoko
3,932 Points

Sorry, the identation got messed up when I copy and pasta-ed the code. I didn't know how to paste a snapshot of the code here.

to get a block of code to appear as code

  1. on the first line type 3 backticks characters followed by the word python
  2. on the following lines insert your code
  3. then at the end of your code type 3 backticks characters again to close the code block

Here is how it is referenced in the Markdown Cheatsheet: Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

On many keyboards, the backtick is located with the ~ which is a key above the tab key

team = []

prompt = input("Would you like to add a player to the list? (yes/no) ") 

while prompt.lower() == 'yes' or 'y': 
    playerName = input("Enter the name of the player to add to the team: ") 
    team.append(playerName) 
    prompt = input("Would you like to add another player? (yes/no) ")

print("There are {} players on the team".format(len(team)))
Harris Handoko
Harris Handoko
3,932 Points

Oooo! Thanks, Frank! That was helpful!

2 Answers

You will need to re-evaluate your while statement. Python prefers you to be explicit. Without giving you the direct answer, consider how if statements work when using more than one conditional.

For example when combining comparison during an if statement you would write:

# correct and very explicit
if some_variable == "test_string" or some_variable == "different_string":
    print("some variable matched what I was looking for")

# incorrect as technically it means something else
if some_variable == "test_string" or "different_string":
    print("second string after the or operator is 'truthy' because it exists and will always be True")
    print("since or accepts True on either side this will never be False")

# this is a good thing because you might want to use something like
if some_variable == "test_string" and this_thing_is_true:
    print("You can use a variable, here called 'this thing is true' to hold a value")
    print("because this test has and then both sides must be true")

see what happens if you test the boolean of a string a follows:

print(bool("y"))
Harris Handoko
Harris Handoko
3,932 Points
while prompt.lower() == 'yes' or prompt.lower() == 'y': 
    playerName = input("Enter the name of the player to add to the team: ") 
    team.append(playerName) 
    prompt = input("Would you like to add another player? (yes/no) ")

print("There are {} players on the team".format(len(team)))

Yes, now this works. Thank you, Frank.