Python - limiting input to certain characters

Joined
Jan 18, 2025
Messages
1
Reaction score
0
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")
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
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")
The or 'y' part is always true, which means the loop will not terminate as expected.
Check if parking_pass is not equal to both 'n' and 'y'. The condition should use the and operator instead of or.
Hope this helps.
 
Joined
Jul 4, 2023
Messages
575
Reaction score
77
BTW,
Python:
parking_pass = str(input())

'''
   when you write str(input()), you are performing an unnecessary type conversion 
   because the result of input() is already of type str.
'''
parking_pass = input()

user friendly ;)
Python:
while parking_pass not in ('y', 'Y', 'n', 'N'):

# or
    
while parking_pass.lower() not in ('y', 'n'):
 
Joined
Sep 21, 2022
Messages
226
Reaction score
36
The interesting thing to me about this error is that when read out loud, while parking_pass != 'n' or 'y' sounds right. Its the way an english speaker would phrase it.

Makes me wonder what other statements read perfectly fine, but are actually completely wrong.

The other thing is, why is there an else: in your while statement?
 
Joined
Jul 4, 2023
Messages
575
Reaction score
77
when read out loud, while parking_pass != 'n' or 'y' sounds right. Its the way an english speaker would phrase it.
However, this logic applies to spoken language.
In this case, we are dealing with a programming language, and in Python, this is correct according to its syntax and logic.
Python:
# You need to explicitly compare the variable parking_pass with both values.
while parking_pass != 'n' and parking_pass != 'y':
 
Last edited:
Joined
Sep 21, 2022
Messages
226
Reaction score
36
What I mean VB, is that if I was skimming a 200 line program, looking for a logic error, this kind of thing would not jump out at me.

Like those puzzles where they ask you to find the mistake in the the sentence and most people can't see the extra "the".
 
Joined
Jul 4, 2023
Messages
575
Reaction score
77
I completely agree with you. In my response, I mainly meant to use your statement to explain the issue to the OP based on your thoughts. I apologize for being imprecise in my reply, which might suggest that I consider your line of thinking to be somewhat incorrect. 😇
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top