Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create exception handler
#1
All:
I'm a newbie to python, but I have lots of experience in other programming languages...

I want to build an exception handling routine... If the year entered is not within a range of years, then signal an error and loop back to try again...

Any ideas?

Thanks in advance,
newbie in Newark
Reply
#2
What have you tried? If you're experienced in other languages with exception handling, this shouldn't be too hard.
Reply
#3
Sorry, but I don't want to bandy back and forth about what I've tried... what I've tried obviously is not working! I'm sure it's a stupid mistake... Also, I do not need criticism or sarcastic remarks about whether or not I'm experienced. If you have a quick & dirty code snippet or solution to my problem, then post it. I thought this forum was supposed to help both simple and complex problems. If you can't offer or don't want to offer a solution in short order, then fine... Perhaps I've forgotten what a forum is supposed to be about. THANKS!
Reply
#4
oradba4u, the forum is for helping and learning for all members and it is asked that people show their code or snippet and errors where they have issues along with showing their attempts to resolve the issue. By following these steps it helps the other members to try to point you in the best direction for help and it helps to provide the other members with a basic understanding of where you might be in your abilities in coding python to help you or any other member.

Criticisms or sarcastic remarks are not what the forum is for so there is no need to assume such however members do not do the work for others, we help the member to learn how to do it for themselves through direction or examples.

Please post your current code along with any alterations you may have tried along with the error(s) received when the code was run. That will help others to help you.

As for an exception handler... it would generally be simplistic but showing your code will help in the layout of the resolve you're after.
"Often stumped... But never defeated."
Reply
#5
(May-22-2020, 05:11 AM)oradba4u Wrote: I want to build an exception handling routine... If the year entered is not within a range of years, then signal an error and loop back to try again...
From your summarily description I'm not sure you need to create your own exception handler for this. I believe a simple try...except construct may be sufficient.
Some forum members already found nice solutions. Look for example at [split] Python for Everybody 5.2 assignment.
Reply
#6
Given a range of a min_year and a max_year, raise an exception if value entered is not an integer, OR the value does not fall in the specified range (in this case 1959-2007)

Here's what I've tried (close, but no cigar)
---------------------------------------------------
min_year=1959
max_year=2007

display_string = "What Year? (" + str(min_year) + "-" + str(max_year) + ")"

while True:
    try:
        requested_year = int(input(display_string))
        requested_year >= min_year and requested_year <= max_year
    except:
        print ("Invalid input or value out-of-range... Please re-try")
    else:
        break

print("Continue Code Execution")
Reply
#7
Something like this.
Could raise an exception for out of range,but then goes out of loop trouble and with try again functionality.
It's possible with an other try:except to catch the raised error,but don't see the need in this case.
Look into f-string that used in code.
min_year = 1959
max_year = 2007
display_string = f"What Year? ({min_year} - {max_year}) "
while True:
    try:
        requested_year = int(input(display_string))
        if not min_year <= requested_year <= max_year:
            print("Year out of range,try again")
        else:
            break
    except ValueError:
        print('Not a number,try again')

print("Continue Code Execution")
print(requested_year)
Reply
#8
That helped immensely... Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error handler appears to be turned off. How do I turn it back on? jpotter0 0 563 Nov-26-2022, 11:44 AM
Last Post: jpotter0
  create my exception to my function korenron 2 757 Nov-09-2022, 01:50 PM
Last Post: korenron
  how to get process handler? maiya 1 1,656 Feb-23-2021, 07:34 AM
Last Post: maiya
  Custom logging handler looping continuously linuxaddikt 0 1,756 Mar-12-2020, 06:58 PM
Last Post: linuxaddikt
  During handling of the above exception, another exception occurred Skaperen 7 26,722 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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