2

So I am trying for my program to print the message "That is not an integer!" if the user inputs something that is not an integer basically, I thought this was how you would do that but apparently isn't, could anyone tell me what I am doing wrong please?

user_number = input() if type(user_number) != int: print("That's not an integer number.") 
5
  • Is this python 2 or 3? Commented Dec 5, 2017 at 17:02
  • Use a try/except block to try to convert the string to an integer using int(). Commented Dec 5, 2017 at 17:02
  • use user_number.isdigit() will return True or False Commented Dec 5, 2017 at 17:03
  • Python 2.7.10 / Commented Dec 5, 2017 at 17:04
  • @stack doesn't work if you want to accept negative Commented Dec 5, 2017 at 17:05

1 Answer 1

3

You could try to convert the input to integer with try/except:

user_number = input() try: int(user_number) except: print("That's not an integer number.") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.