0
Please explain the code below and the type of error:
def my_func(x): assert x>0, "ERROR" print(x) my_func(input())
7 Answers
+ 7
def
assert
+ 3
Line 2:
TypeError: unorderable types: str() > int()
Python doesn't implicitly compare strings and numbers (in what order does "Robert" come in relation to 437?) ... you have to cast/convert. In many languages, input is a string type.
assert appears to be a special case -- by which I mean a custom error; the idea appears to be the same.
Try this in CodePlayground:
help('assert') # just the simple form explanation
+ 3
1. def is answer
2. assert is second answer
0
Fill in the blanks to define a function that takes one argument. Assert the argument to be positive.
def my_func(x):
assertx > 0, "Error!"
print(x)
0
def my_func(x):
assertx > 0, "Error!"
print(x)
0
def
my_func(x):
assert
x > 0, "Error!"
print(x)
- 1
poo