0

So my question is: Ive created a while with a if-elif-else clause in the while loop. I prompt the user to enter y or n, case insensitive. But I want the loop to keep looping until they enter either a y,Y,n,N to print out the corresponding string. But when I enter any string or integer it keeps looping like I want but when I enter the y,Y,n,N it still keeps looping and doesnt print the strings I want it too.

var = input("Enter Y/N:")


while var != 'y' or var != 'Y' or var != 'n' or var != 'N' :
    if var == 'y' or var == 'Y' :
        print("You said yes")
        break
    elif var == 'n' or var == 'N':
        print("You said no")
        break
    else:
        input("Enter Y/N:")
5
  • 1
    Move the input into your while loop and define var = '' at the top (before the loop) Commented Oct 17, 2015 at 2:12
  • Your if conditions will always jump to input(...) Commented Oct 17, 2015 at 2:12
  • Yea Im wondering why they wont evaluate. If I delete the input function then I can get the desired conditions to print. But then it wont loop. Commented Oct 17, 2015 at 2:15
  • 1
    Because you are checking if input is yes or no and enter the while body only if they are not, then you are checking whether the input is yes/no which will never be true. Commented Oct 17, 2015 at 2:18
  • So I put the var = input("Enter Y/N:") in the else clause and it works. Thanks Commented Oct 17, 2015 at 2:18

1 Answer 1

2

Based on your post it's seems you want a chance to skip the loop based on user's input. To do that you can use this algorithm:

var = input("Enter Y/N:")
if var in ['y', 'Y']:
    print("You said yes")
elif var in ['n', 'N']:
    print("You said no")
else:
    while True :
        var = input("Enter Y/N:")
        if var in ['y', 'Y']:
            print("You said yes")
            break
        elif var in ['n', 'N']:
            print("You said no")
            break

Why not write while with a condition like var not in ['y', 'Y', 'n', 'N']?

var = input("Enter Y/N:")
if var in ['y', 'Y']:
    print("You said yes")
elif var in ['n', 'N']:
    print("You said no")
while var not in ['y', 'Y', 'n', 'N']:
    var = input("Enter Y/N:")
    if var in ['y', 'Y']:
        print("You said yes")
    elif var in ['n', 'N']:
        print("You said no")

We could switch the else statement for something more explicit over the while statement this will work too but as you can see our code seems to have more cut-points. The only problem is that we have increased the cut-point with redundant routines.

In my opinion is better and more readable to implement the input inside the loop.

for python 3.x

while True :
    var = input("Enter Y/N:")
    if var in ['y', 'Y']:
        print("You said yes")
        break
    elif var in ['n', 'N']:
        print("You said no")
        break

for python 2.x

while True :
    var = raw_input("Enter Y/N:")
    if var in ['y', 'Y']:
        print("You said yes")
        break
    elif var in ['n', 'N']:
        print("You said no")
        break

Now we have a very tiny code doing the same thing.

I notice too that you just want to read the letters {y,n}. The case sensitive is a definition useful for the user only, the cases will produce the same answer. So you could just lower or upper all the inputs to simplify your conditions

for python 3.x

while True :
    var = input("Enter Y/N:").lower()
    if var == 'y':
        print("You said yes")
        break
    elif var == 'n':
        print("You said no")
        break

for python 2.x

while True :
    var = raw_input("Enter Y/N:").lower()
    if var == 'y':
        print("You said yes")
        break
    elif var == 'n':
        print("You said no")
        break
Sign up to request clarification or add additional context in comments.

3 Comments

Yep thats another solution too. So I can create a list in a if statement?
didn't get you mate! can you rephrase?
My question has been answered cause you obviously created a list and it executes lol. Thanks

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.