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

zoltans
zoltans
2,547 Points

Is this a right way to solve this challenge?

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

print("{}, do you understand Python while loops?".format(name))

answer = input("(Enter yes/no) ")

while answer.lower() != "yes":

print("Oh, {}, while loops in Python repeat as long as a certain Boolean condition is met.".format(name))

print("{}, now do you understand while loops?".format(name))

answer = input("Enter yes/no ")      

else:

print("Congratulation {}, for grasping while loops!".format(name))

Am I using too many print functions? Isn't 'else:' necessary? Thanks for your input! :)

1 Answer

Steven Parker
Steven Parker
230,230 Points

An "else" is only needed after a "while" when you want to distinguish between the loop ending on its test condition vs. ending because of a "break". Since no "break" statements are used (or needed) in this loop, the "else" is also not needed (but it doesn't hurt).

I'm not sure what you mean by "too many print functions". As long as each one prints out a message that is pertinent to the program, I think you can consider that to be the right number of them (as appears to be the case here).

So overall, yes, this appears to be a good solution. :+1:

zoltans
zoltans
2,547 Points

Thank you Steven, for taking the time to answer me! Very kind of you.