Help with python, how make int from string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help with python, how make int from string

I don't want to write if int(x) > 0 every time, how i can rewrite this? x= input() Int(x)// error ? if x >0: print("optimist") else: print ("pessimist") if x == 0: print ("realist")

27th Nov 2018, 8:39 AM
Barbaring0
Barbaring0 - avatar
2 Answers
+ 3
Here is the answer : x= input() if int(x) >0: print("optimist") else: print ("pessimist") if x == 0: print ("realist") Take care, that was not "Int" but "int". Alternative solution : x= int(input()) if x > 0: print("optimist") else: print ("pessimist") if x == 0: print ("realist")
27th Nov 2018, 8:54 AM
Geoffrey L
Geoffrey L - avatar
+ 2
I think you need to assure the user will input only numeric values. I would do it like this: x=input() try: x=int(x) if x>0: print('optmist') elif x<0: print('pessimist') else: print('realist') except: print('You input an invalid information')
5th Dec 2018, 11:31 AM
Silas Vlr
Silas Vlr - avatar