Python Forum
Manually raising two error types with a function that computes sqfeet to sqmeters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manually raising two error types with a function that computes sqfeet to sqmeters
#5
How am I able to raise the TypeError when entering a non numerical input? Because of the float(input .... in line 8 a value error is automatically raised. Thanks!
# Area Unit Converter


from configuration import ERROR_NEGATIVE, ERROR_NAN
RATIO_M2F = 10.764
# LIBRARY: function declaration part
# put definitions of functions here
a_value = float(input("Enter area in square feet: "))
def check_one(a_value):
    if float(a_value) < 0:
        raise ValueError(ERROR_NEGATIVE)
    elif type(a_value) != float:
        raise TypeError(ERROR_NAN)


def sqfeet_to_sqmeters(a_value):
    check_one(a_value)
    return a_value / RATIO_M2F ** 2

check_one(a_value)
print(sqfeet_to_sqmeters(a_value))
Yoriz write Nov-12-2021, 07:57 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Raising Errors - by sean1 - Nov-12-2021, 01:30 AM
RE: Raising Errors - by deanhystad - Nov-12-2021, 02:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a generator that computes squares of first 20 natural numbers mdshamim06 1 9,031 Oct-01-2019, 10:38 AM
Last Post: buran

Forum Jump:

User Panel Messages

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