+ 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")
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")
+ 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')