+ 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?

26th Jan 2018, 7:32 AM
Boris Stricky
Boris Stricky - avatar
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")
26th Jan 2018, 8:07 AM
Oma Falk
Oma Falk - avatar
+ 3
assert throws assert error. assert type(num) == str , "this is a value error" but it is only a message .
26th Jan 2018, 9:00 AM
Oma Falk
Oma Falk - avatar
+ 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?
26th Jan 2018, 8:49 AM
Boris Stricky
Boris Stricky - avatar