Advertisement
Guest User

Stub for int/float

a guest
Jun 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # - Get the input value
  4. x = input('Enter a number')
  5.  
  6. # - Determine what it is. Try float first, because an int value
  7. #   will also succeed.
  8. is_float = False
  9. try:
  10.     check_float = float(x)
  11.     # - If no error is raised, it's a float value
  12.     is_float = True
  13. except ValueError:
  14.     # - it can't be converted to an float
  15.     pass
  16. # - Now try as an int
  17. is_int = False
  18. try:
  19.     check_int = int(x)
  20.     # - If no error is raised, it's an int value
  21.     is_int = True
  22. except ValueError:
  23.     # - it can't be converted to an int
  24.     pass
  25.  
  26. # - Display results as needed. Note that is_int AND is_float
  27. #   might both be false!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement