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
var = ''at the top (before the loop)ifconditions will always jump toinput(...)yesornoand enter the while body only if they are not, then you are checking whether the input is yes/no which will never be true.