1

I want my code to take a user input, then prompt the user to continue, if they answer 'y' then it asks for another input, if they answer 'n' the program stops and if they type any other characters it simply continues to prompt them until they enter a 'y' or 'n'.

As the code shows I'm trying to use a while loop to continuously prompt the user until they enter a 'y' or 'n'. However when I reach the while loop it does not stop when a 'y' or 'n' is entered.

def test():

    number = input('Input a number then press enter:')    
    print(number)
    prompt = input('Continue (y/n)? ')

    if prompt == 'y':
        number = input('Input a number then press enter:')
        print(number)
        prompt = input('Continue (y/n)? ')
    elif prompt == 'n':
        pass

    else:
        while prompt != 'y' or 'n':
        prompt = input('Continue (y/n)? ')
1
  • 2
    Please fix the indentation. It matters. Commented Apr 25, 2015 at 3:58

2 Answers 2

6

This is not how or works:

while prompt != 'y' or 'n':

You probably meant:

while prompt != 'y' or prompt != 'n':

Your version ors prompt != 'y' and 'n', which always yields at least the last truth-y value ('n').

The full code:

def test():

    number = input('Input a number then press enter:')    
    print(number)
    prompt = input('Continue (y/n)? ')

    if prompt == 'y':
        number = input('Input a number then press enter:')
        print(number)
        prompt = input('Continue (y/n)? ')
    elif prompt == 'n':
        pass

    else:
        while prompt != 'y' or prompt != 'n':
            prompt = input('Continue (y/n)? ')

To do these kind of input loops I normally use while True with break:

def test():

    prompt = 'y'
    while True:
        if prompt == 'y':
            number = input('Input a number then press enter:')
            print(number)
        elif prompt == 'n':
            break
        prompt = input('Continue (y/n)? ')
Sign up to request clarification or add additional context in comments.

5 Comments

Okay thanks, I have changed so there is a second prompt != n, however when I run it is still stuck in the while loop?
Can you use a debugger? Can you print prompts value to figure out what's going on?
Would I be able to solve my problem using exception handling?
When I reach else, it executes the while loop but I want it to go back and execute the if statement if 'y' is input and elif if 'n' is input. It just continues to run the while loop at the moment.
@MrWallace see my edit of how I'd do it. The corrected code worked for me, but I'm too tired to follow different execution paths. Good luck.
-2

you have to use raw_input() in order to take non-numerical variables as input.

1 Comment

No. This is Python 3. See the tag?

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.