Still getting Type and Value Error
def my_divider(a,b):
try:return a/b
except ZeroDivisionError:
return"Error: You cannot divide a number by 0"
except TypeError:
a=float(a)
b=float(b)
return a/b
except ValueError:
return"Please enter an integer"
var1=10
var2='dog'
print(my_divider(var1,var2))
I'll echo what @dkamins is saying here but I need the rep so I'll take a stab here.
The code you're cooking looks so close, you'll kick yourself when you see it. Great use of the try except clause! You're looking like a python pro already. Yes, the error occurs when you try to assign b = float(b)
when isinstance(b,str)
but I see, I think, you trying to catch that exception. But since it is inline with a separate try-except clause, it is behaving how you're seeing. So, something like:
def my_divisor(a,b):
try:
return a/b
except ZeroDivisionError:
print("You cannot divide by the number 0")
return None
except TypeError:
try:
a = float(a)
b = float(b)
return a/b
except ValueError:
print('Error converting type of a, b to numeric. Just put in some numbers, pls')
return None
Be careful!!! We didn't handle something here. What would my_divisor(40,"0")
return? I think you got it from here friend!