Advertisement
gruntfutuk

Check input is a float

Mar 27th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. ''' calculate basic laws of motion '''
  2. import random
  3. import sys
  4. import os
  5.  
  6. def isfloat(value):
  7.     ''' check argument string contains a valid float or integer, return True or False accordingly '''
  8.     if value.isnumeric(): return True
  9.     try:
  10.         float(value)
  11.     except ValueError:
  12.         return False
  13.     else:
  14.         return True
  15.  
  16. def getfloat(prompt):
  17.     ''' display argument prompt, keep going until valid numeric input entered, return float cast of input '''
  18.     while True:
  19.         user_entry = input(prompt)
  20.         if isfloat(user_entry):
  21.             return float(user_entry)
  22.         else:
  23.             print('That is not a valid input. Try again.')
  24.  
  25. print("Laws of motion")
  26. print("Calculate if v=v0+at")
  27.  
  28. v0 = getfloat('Enter v0: ')
  29. a = 9.8
  30. t = getfloat("Enter t: ")
  31. v = v0 + a * t
  32. print('v = ', v)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement