+ 1
Can assert throw other error types?
for example assert: type(input) == str Can i Make it throw a ValueError if the input is integer?
3 Respostas
+ 3
input is a function that always returns a string. even if the input is a digit.
the type will change with int(input).
but here you will be too late with an assert because if the conversion fails, the prog will dump.
maybe
inp = input()
try:
int(inp)
except:
Print("no integer")
+ 3
assert throws assert error.
assert type(num) == str , "this is a value error"
but it is only a message .
+ 1
Thanks Oma,
but my question here was more related to assert errors, and not necessarily input. for clarity maybe this type of code would be better to ask:
num = 1
assert type(num) == str
this code will throw an AssertError, right?
can I write the code so that I'd throws a ValueError instead?