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 trialEnes artun
Courses Plus Student 2,364 PointsHow to stop the function when lower case Y or N inputted?
Hi! I managed to do the challenge but I was wondering how can I stop the execution of the script when lower case Y or N given? When I type upper Y or lower case Y script goes on regardless of the answer. Same with the N.
name = input("What's your name? ")
question = str(input(f'Do you understand while loops {name}? Y/N '))
# TODO: Ask the user by name if they understand Python while loops question == 'Y':
while question == 'Y':
print (f'Good job {name}')
break
while question == 'N':
print(f'Ok, {name}, while loops in Python repeat as long as a certain Boolean condition is met.')
answer = input(f'{name}, do you understand Python while loops now? Y/N ')
if answer == 'N':
print(f'Ok, {name}, while loops in Python repeat as long as a certain Boolean condition is met.')
print('Do you understand now? Y/N ')
elif answer == 'Y':
break
print('Congrats. You understand while loops now.')
# TODO: Write a while statement that checks if the user doesn't understand while loops
# TODO: Since the user doesn't understand while loops, let's explain them.
# TODO: Ask the user again, by name, if they understand while loops.
# TODO: Outside the while loop, congratulate the user for understanding while loops
1 Answer
Steven Parker
231,268 PointsThe loop is being controlled by the variable "question", but when you ask for input the variable "answer" is being assigned.
If you use the same variable for both, the loop will end when the answer is not "N".