Please explain the code below and the type of error: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain the code below and the type of error:

def my_func(x): assert x>0, "ERROR" print(x) my_func(input())

4th Oct 2017, 2:04 PM
emi
7 Answers
+ 7
def assert
7th Apr 2020, 3:16 PM
Ahmed Ansari
Ahmed Ansari - avatar
+ 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
4th Oct 2017, 2:31 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
1. def is answer 2. assert is second answer
2nd Mar 2021, 3:21 PM
v manojbabu
v manojbabu - avatar
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)
3rd Aug 2019, 5:25 AM
GYANENDRA PAL SINGH
GYANENDRA PAL SINGH - avatar
0
def my_func(x): assertx > 0, "Error!" print(x)
25th Apr 2020, 8:52 PM
Shivam jaiswal
0
def my_func(x): assert x > 0, "Error!" print(x)
10th Jul 2021, 1:55 AM
Bimsara Senavirathna
Bimsara Senavirathna - avatar
- 1
poo
12th Mar 2020, 12:27 PM
akshayan