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
#1
I keep getting TypeError: unsupported operand type(s) for /: 'str' and 'float'. How do I manage to raise the errors successfully as well as removing this reoccurring TypeError? Please help!!! Thank you in advance!!

def sqfeet_to_sqmeters(a_value):
    answer_in_sqmeters = a_value / 10.764 ** 2
    try:
        if type(a_value) == float:
            return answer_in_sqmeters
    except:
        try:
            if type(a_value) != float:
                raise TypeError(ERROR_NAN)
        except TypeError:
            if a_value < 0:
                raise ValueError(ERROR_NEGATIVE)
a_value = input("Enter area in square feet: ")
print(sqfeet_to_sqmeters(a_value))
Reply
#2
indentations are correct in my PyCharm file- sorry about that
Reply
#3
Please wrap code in Python tags. There is a button in the editor window for doing this.

I am guessing this is your code. See my comments:
def sqfeet_to_sqmeters(a_value):
    answer_in_sqmeters = a_value / 10.764 ** 2   # Will always raise TypeError because a_value is a str
    try:  # Why this try?  The enclosed code cannot raise an exception
        if type(a_value) == float:  # If you got here a_value had to be a number.  Why test?
            return answer_in_sqmeters
    except:
        try:
            if type(a_value) != float:
                raise TypeError(ERROR_NAN)  # Raising this exception get to the if statement 2 lines down
        except TypeError:
            if a_value < 0:  # Only way to get here is if a_value is not a float.
                raise ValueError(ERROR_NEGATIVE)

a_value = input("Enter area in square feet: ")
print(sqfeet_to_sqmeters(a_value))
What is your code supposed to do?
Reply
#4
new code- Line 1 starts with the #

# Area Unit Converter


from configuration import ERROR_NEGATIVE, ERROR_NAN

# LIBRARY: function declaration part
# put definitions of functions here

def sqfeet_to_sqmeters(a_value):
    answer_in_sqmeters = a_value / 10.764 ** 2
    try:
        a_value = float(a_value)
    except ValueError:
        a_value < 0
    if type(a_value) != float:
        raise TypeError(ERROR_NAN)
    else:
        return answer_in_sqmeters

a_value = input("Enter area in square feet: ")
print(sqfeet_to_sqmeters(a_value))
Console:
Error:
Enter area in square feet: -2 Traceback (most recent call last): File "/Users/sean/PycharmProjects/U04_Functions1/Programs/SqFeet/task.py", line 21, in <module> print(sqfeet_to_sqmeters(a_value)) File "/Users/sean/PycharmProjects/U04_Functions1/Programs/SqFeet/task.py", line 10, in sqfeet_to_sqmeters answer_in_sqmeters = a_value / 10.764 ** 2 TypeError: unsupported operand type(s) for /: 'str' and 'float' Process finished with exit code 1
Yoriz write Nov-12-2021, 07:55 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
#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
#6
Wrap code in python tags please. There is a button in the editor for doing this.

What is your question. Do you not know how to raise an exception or do you not know how to identify if a str can be converted to a number?

This sounds like some kind of homework. What are the instructions for the assignment?
Reply
#7
Wrap line 8 in a try...except block. However, if the value entered is not convertible to a float, what do you want to do? Loop back and ask for entry again? If so, you need to think about how you might do that (and that problem has been addressed here before).

If instead you are to raise an error when someone enters a string that cannot be converted, Python does that for you when you try to float().

You can check to see if negative you would use something like
if x<0:
    raise foo
Reply
#8
What is the homework assignment? What you are doing doesn't make much sense and I wonder which applies:
A) Assignment is weird
B) You don't understand the assignment
C) Your code doesn't reflect the assignment.

Your code will not get past line 8 if the user types "a" when asked to enter the area. Are you supposed to test the input and raise an exception if it can't be converted to a float, or are you supposed to catch the exception and raise a different exception? Either fall into category A) Assignment is weird.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a generator that computes squares of first 20 natural numbers mdshamim06 1 8,958 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