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)
#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


Messages In This Thread
RE: Try,Except,Else to check that user has entered either y or n (Code block pasted) - by buran - Jun-29-2021, 03:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 14,931 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Code to check folder and sub folders for new file and alert fioranosnake 2 2,976 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 3,136 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  Help with User Interface design code ai_masti 0 2,340 Nov-19-2020, 05:58 PM
Last Post: ai_masti
  Can somebody check what is wrong with my code? hplus_liberation 4 3,586 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 3,701 Aug-13-2020, 02:15 PM
Last Post: perfringo
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 4,683 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How to reuse positional arguments entered in terminal. rcmanu95 1 2,568 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 3,032 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  how to divide one big equation entered as a string to small chunks gungurbuz 1 2,126 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