Advertisement
furas

Python - input int and float

Apr 8th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def get_float(text_input, text_error, min_val):
  2.     while True:
  3.         try:
  4.             value = float(input(text_input))
  5.             if value >= min_val:
  6.                 return value
  7.             print(text_error)
  8.         except ValueError:
  9.             print('You have to enter float number')
  10.         except KeyboardInterrupt:
  11.             print("You can't exit now.")
  12.            
  13. def get_int(text_input, text_error, min_val):
  14.     while True:
  15.         try:
  16.             value = int(input(text_input))
  17.             if value >= min_val:
  18.                 return value
  19.             print(text_error)
  20.         except ValueError:
  21.             print('You have to enter integer number')
  22.         except KeyboardInterrupt:
  23.             print("You can't exit now.")
  24.    
  25. flour = get_int('Flour: ', 'Flour must not be less than 50kg', 50)
  26. print('value:', flour)
  27.  
  28. flour = get_float('Flour: ', 'Flour must not be less than 50kg', 50)
  29. print('value:', flour)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement