Python Forum
Checking for particular letter in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking for particular letter in python
#1
I am trying to limit a user's input to either the letter y or n. I am using this code. However, even when I enter n or y it still loops. I would appreciate someone letting me know where I have gone wrong! Thank you very much.

while parking_pass != 'n' or 'y':
        print ("Error Contains illegal characters")
        print("Would you like a free parking pass y/n?")
        parking_pass = str(input())    
else: 
        print("Your parking pass has been added to your order")
Gribouillis write Jan-18-2025, 07:24 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
A while loop exits when a condition is True.

Below, one or the other condition will always be False, because your condition can't be y and n, so the while loop will never exit.

while parking_pass != 'n' or parking_pass != 'y':
I can think of 2 ways:

1. Use a list with possible answers:

answers = ['n', 'y', 'maybe']
answer = 'x'
while not answer in answers:
    answer = input('Would you like a free parking pass y/n? Please enter n for no or y for yes. ')
2. A while loop continues until a condition is True. True is always True, so the while loop below will continue forever. Then set an if clause to catch the True condition and break.

while True:
    parking_pass = input('Would you like a free parking pass y/n? Please enter n for no or y for yes. ')
    if parking_pass == 'n' or parking_pass == 'y':
        break
Reply
#3
Alternatively and maybe with more useful information for user:

allowed = ["y", "n"]

while (answer := input("Would you like a free parking pass y/n?\n")) not in allowed:
       print(f"Expected {' or '.join(allowed)} but {answer!r} was entered")
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
There is a problem with the comparison
while parking_pass != 'n' or 'y':
a or b returns a if a bool(a) == True, else it returns b.

If parking pass != 'n', then your comparison returns True. If pass == 'n', the comparison returns 'y'. True and bool('y') are both True, so the while loop never exits.

You could write the code like this.
while not (parking_pass == 'n' or parking_pass == 'y'):
Now in the comparison a or b, a is "parking_pass == 'n'" and b is "parking_pass == 'y'. If you don't like the not(), you can write it like this:
while parking_pass != 'n' and parking_pass != 'y':
Or we can use a test for inclusion.
while parking_pass not in ('n', 'y'):
Fixing the problem:
parking_pass = input("Would you like a free parking pass y/n? ")
while parking_pass not in ('n', 'y'):
        print("Error Contains illegal characters")
        parking_pass = input("Would you like a free parking pass y/n? ") 
else: 
        print("Your parking pass has been added to your order")
Duplicate code should be avoided.
while (parking_pass := input("Would you like a free parking pass? ")) not in ('n', 'y'):
    print("Enter y or n.")
else:
    print("Your parking pass has been added to your order")
You are not using while/else correctly. while/else only makes sense when you can break out of the while. The else: code only executes if the while condition is false. If you break out of the loop the else code is not executed. You could write you code like this to only print parking pass added message if the user enters 'y'.
while (parking_pass := input("Would you like a free parking pass? ")) != 'y':
    if parking_pass == 'n':
        break
    print("Enter y or n.")
else:
    print("Your parking pass has been added to your order")
This looks odd. The meaning of this code is clearer.
while (parking_pass := input("Would you like a free parking pass? ")) not in('n', 'y'):
    print("Enter y or n.")
if parking_pass == 'y':
    print("Your parking pass has been added to your order")
If you are using python 3, input() returns a str. There is no need for str(input()). If you are using python 2, why? And if you are using python 2 you should use raw_input().
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020