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 The Challenge

Juan Perez
PLUS
Juan Perez
Courses Plus Student 8,944 Points

While loops Challenge. I got a different solution.

I got a different solution than the video. Is it still a good and valid one? I had trouble with the last TODO. How and what would've done to improve it?

import sys

name = input("What's your name? ")

# TODO: Ask the user by name if they understand Python while loops
understanding = input("Hello, {} do you understand while loops?  \n(Enter yes/no)".format(name))

# TODO: Write a while statement that checks if the user doesn't understand while loops
while understanding:
    if understanding == 'no':
# TODO: Since the user doesn't understand while loops, let's explain them.
        print("Ok, {}, while loops in Python repeat as long as a certain Boolean condition is met.".format(name))
# TODO: Ask the user again, by name, if they understand while loops.
        understanding = input("{}, now do you understand Python while loops? ".format(name))

# TODO: Outside the while loop, congratulate the user for understanding while loops
    elif understanding == 'yes':
        print("That's great, {}. I'm pleased that you understand while loops now. That was getting repetitive.".format(name))
        sys.exit()

1 Answer

Hi, Juan. Your code looks like it will give the expected output. Working code is the beginning of great code! In this case, they want a simpler loop, keeping your code DRY.

You have an extra level of code. while understanding always evaluates to true, because it is not a boolean value. It is a string value, and a string value is always a truthy value. So, your code says, "always do this, no matter what". It would work exactly the same if if it said while true. True is always true, so you are creating an infinite loop with conditions to break out of it.

A simpler way to do this is to skip the infinite loop.

# ... other code here

while understanding.lower  == 'no'
    # print statement re-explaining the concept
    # re-ask question

# print statement congratulating them on understanding outside of while loop.
# if, elif, or else clauses are not needed

Just something to think about - in an actual user experience, this would need a try-except block checking for anything other than "yes" and "no," because things like "Nope" and "huh?" would be taken as "Yes," because they are not "no". But, none of that is necessary to understand an while loop. It is just a pattern to start thinking about.

Hope that helps! Post back if I can clarify further.