Patrick
Patrick

Reputation: 77

Printing an output string in a while loop in Python

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:")

Upvotes: 0

Views: 8986

Answers (1)

Moraes
Moraes

Reputation: 495

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

Upvotes: 2

Related Questions