Python Forum
Avoiding traceback not through input filtering
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoiding traceback not through input filtering
#6
I think the try/except solution would work better this way:

n = input("What term of the Fibonacci Sequence would you like to know? ")
 
try: 
    n=int(n)
except ValueError: 
    raise TypeError("n must be a positive integer.")
if n<0:
    raise ValueError("n must be positive.")
This keeps the exception checking narrow (to avoid catching errors you didn't expect), and allows you to tailor your error to the specific problem with the input.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Avoiding traceback not through input filtering - by ichabod801 - Oct-16-2019, 04:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoiding Re-login Goodsayan 0 1,435 Sep-09-2021, 01:53 PM
Last Post: Goodsayan
  Avoiding too many if's Ted_Toad 6 2,728 Sep-17-2020, 07:18 PM
Last Post: Ted_Toad
  What is the better way of avoiding duplicate records after aggregation in pandas ? jagasrik 0 1,779 Aug-30-2020, 05:26 PM
Last Post: jagasrik
  Avoiding empty line in writing process csv file go127a 4 9,909 May-10-2019, 01:33 PM
Last Post: go127a

Forum Jump:

User Panel Messages

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