0

This is my loop:

sure = input('Are you sure that you wish to reset the program?(y/n)')
while sure != 'y' or sure != 'n':
    sure = input('Please awnser y or n, Are you sure that you wish to reset the program?(y/n)')

The loop carries on looping even if y or n are entered.

3 Answers 3

5

Change the condition to

while sure != 'y' and sure != 'n':

Your condition as written will always be True no matter what they enter. An alternative would be

while sure not in ('y','n'):
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used the not in suggestion
3

You need to do and instead of or . When doing or it will continue to the loop if sure is not y as well n , but sure cannot be both at the same time, hence it loops forever.

Example -

sure = input('Are you sure that you wish to reset the program?(y/n)')
while sure != 'y' and sure != 'n':
    sure = input('Please awnser y or n, Are you sure that you wish to reset the program?(y/n)')

Comments

3

The problem is in your logical expression:

sure != 'y' or sure != 'n'

Using De Morgan's Law, this could be rephrased as:

!(sure == 'y' and sure == 'n')

Obviously, sure can never be both 'y' and 'n', so this doesn't work. What you want instead is:

sure != 'y' and sure != 'n'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.