-4

I’m new at programming. How can I get an error message if I input a string, using an if else statement in Python.

x = int(input("Enter the no."))

if (x>5):
    print (x ,'Is greater than 5')
elif (x==str):enter code here
    print('Incorrect input')
elif (x==5):
    print (x ,'Is equal to 5')
elif (x<5):
    print (x ,'Is less than 5')
else:
    print (x ,'none of the above!')
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
Renz
  • 1
  • 1

1 Answers1

0

I don't know how you're studying Python, but either your source is awful or you're jumping too ahead.

Problems

  1. Converting your input to int
    In Python 3, any user input you receive through the input() function is considered to be a string. Therefore, when you do x = input(), the type of x is string. By wrapping the input() function with the int() function, you're converting this output, which is supposed to be a string, to an int.
    .

  2. When you check conditions, you compare x directly to str, which will not work. str is a string class while x is its instance. If you want to compare the two, you'd obviously have to use the type() function - type(x) == str.

spicypumpkin
  • 1,209
  • 2
  • 10
  • 21
  • Can you recommend good tutorial video that i can download to learn python ? – Renz Feb 09 '17 at 10:39
  • No need to be sorry, I taught myself Python as well so I was just recommending that you take baby steps first. Also no I can't, I initially used Codecademy like everyone else.. lol – spicypumpkin Feb 09 '17 at 10:59