Python Forum
Try,Except,Else to check that user has entered either y or n (Code block pasted)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try,Except,Else to check that user has entered either y or n (Code block pasted)
#1
Hey up, pretty simple query. I just want to check the user has entered either y or n , its going straight to the else: regardless of the input
Thanks for anybody who has a moment to give feedback.

def ask():
    while True:
        try:
            reply = str(input("Do you want another drink Y/N "))
        except:
            while reply.lower() not in ("y","n"):
                print("you have incorrectly entered - pick Y/N")
                continue
        else:
            print('you have correctly entered y or n')
            break
ask()
Reply
#2
input returns a string so there is no need to convert the returned string into a string.
There is nothing to trigger an exception so the except part of your code doesn't happen.
As there is no conversion from the input that is likely to raise an exception you could simply use the following
def ask():
    while True:
        reply = input("Do you want another drink Y/N ").lower()
        if reply not in ('y', 'n'):
            print("you have incorrectly entered - pick Y/N")
        else:
            print(f'you have correctly entered {reply}')
            break
    return reply


ask()
There is a forum post that you might find helpful
Validating User Input
RandomNameGenerator likes this post
Reply
#3
there is no error raised on this line reply = str(input("Do you want another drink Y/N ")), so except block is never executed and it jumps to else block. And input() returns str so there is no need to explicitly convert it with str(input("Do you want another drink Y/N "))

def ask():
    while True:
        reply = input("Do you want another drink Y/N ")
        if reply.lower() in ("y", "n"):
            print('you have correctly entered y or n')
            break
        else:
            print("you have incorrectly entered - pick Y/N")

ask()
RandomNameGenerator likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks Buran and Yoriz for your assistance. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 6,087 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,931 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 2,053 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  Help with User Interface design code ai_masti 0 1,920 Nov-19-2020, 05:58 PM
Last Post: ai_masti
  Can somebody check what is wrong with my code? hplus_liberation 4 2,590 Sep-16-2020, 05:52 AM
Last Post: perfringo
  Please help me to ask to enter again itself when value entered is not a number. sunil422 5 2,610 Aug-13-2020, 02:15 PM
Last Post: perfringo
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 3,561 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How to reuse positional arguments entered in terminal. rcmanu95 1 1,872 Jul-04-2020, 01:00 AM
Last Post: bowlofred
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 2,265 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  how to divide one big equation entered as a string to small chunks gungurbuz 1 1,614 May-28-2020, 03:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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