-2

I'm trying to have a user input an integer, while trying to catch errors if they put in floaters or strings. Any help would be awesome guys!!

try:
    user_input = int(input())
except ValueError:
    print("Please enter a whole number with no decimal points")
except NameError:
    print("user_input not defined")
Phoenix
  • 3
  • 2

1 Answers1

-1

NameError is called when you try to access a variable not declared yet. But in this code, you will always create user_input, so the try block can't go to the nameerror block.

JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
  • If OP is using Python 2 then `NameError` is raised (and caught, so the question is still not clear either way) if a non-existing variable name is entered when `input` is called. – DeepSpace Jul 26 '20 at 00:40