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
#1
This might be a strange question. I have the beginning and the end... but not sure how to connect the two.

I'm writing a program that asks for a positive integer.

How can I make use of this code snippet:
if type(n) != int:
    raise TypeError("n must be a positive integer")
if n < 1:
    raise ValueError("n must be a positive integer")
The preceding line is:
n = input("What term of the Fibonacci Sequence would you like to know? ")
The problem is input takes a string, which means I will always get the TypeError. I want the errors to occur only if a bona fide decimal is entered (e.g. 1.5) or a negative integer or letters. I don't want the input filtered (e.g. automatically changed to a string).

If I can't do this then I can write a workaround to test for integer and/or string... but I wanted to try this different approach.

Thanks!

Never mind. Not sure I know what I'm talking about with this!
Reply
#2
You can do it with a very little change in the code
import ast

n = ast.literal_eval(input("What term of the Fibonacci Sequence would you like to know? "))

if type(n) != int:
    raise TypeError("n must be a positive integer")
if n < 1:
    raise ValueError("n must be a positive integer")
Reply
#3
I just note that with Gribouillis code different ValueError will be raised during evaluation if string of character(s) is entered:

>>>  ast.literal_eval('a')
/.../
ValueError: malformed node or string: /.../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
I doubt it is good coding but it matches the requirement:

n = input("What term of the Fibonacci Sequence would you like to know? ")

try: 
    n=int(n)
    if n<0:
        raise TypeError("n must be a positive integer")
except Exception as e: 
    raise TypeError("n must be a positive integer")
Reply
#5
(Oct-16-2019, 10:00 AM)baquerik Wrote: I doubt it is good coding but it matches the requirement:

n = input("What term of the Fibonacci Sequence would you like to know? ")

try: 
    n=int(n)
    if n<0:
        raise TypeError("n must be a positive integer")
except Exception as e: 
    raise TypeError("n must be a positive integer")

That's basically how I would do it. This question, though... not really sure what I was getting at and I would have deleted the thread if I saw a "delete thread" button. Thanks anyway!
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoiding Re-login Goodsayan 0 1,383 Sep-09-2021, 01:53 PM
Last Post: Goodsayan
  Avoiding too many if's Ted_Toad 6 2,556 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,725 Aug-30-2020, 05:26 PM
Last Post: jagasrik
  Avoiding empty line in writing process csv file go127a 4 9,717 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