Python

Harris Handoko
Harris Handoko
950 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
950 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.

Frank Genova
Frank Genova
Python Web Development Techdegree Student 11,941 Points

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

Frank Genova
Frank Genova
Python Web Development Techdegree Student 11,941 Points
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
950 Points

Oooo! Thanks, Frank! That was helpful!

2 Answers

Frank Genova
Frank Genova
Python Web Development Techdegree Student 11,941 Points

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
950 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.