Ask coding questions

← Back to all posts
(python3) How can I check if an input is a number and if it's greater than 4?
y33t3rs0n (1)

I'm in a bit of a predicament, I'm trying to find if a number is greater than 4 while using ".isdigit()" in an "if" syntax?

Comments
hotnewtop
Coder100 (18893)

No, you have to first convert it into an int or something first, so here's how:

num = ...

try:
  if int(num) > 4:
    print('greater than 4')
  else:
    print('not greater than 4')
except:
  print('not a number')

isdigit will tell you if it is a number, but to turn it into a number, you must use int (or float to support decimals).

However, that is inefficient, because if int tries to convert and it is not a number, it will throw an error. So we utilize that.