Advertisement
gruntfutuk

check input in list

Jun 17th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def find_type(source):
  2.     'function to examine string and determine if it contains'
  3.     'a viable float, integer or string and return appropriate'
  4.     'type accordingly'
  5.     try:
  6.         value = None
  7.         value = float(source)  # try to convert source to float
  8.         value = int(source)  # float worked, let's try integer
  9.     except ValueError:  # not float or integer
  10.         if not value:  # cast to float failed so neither float nor string
  11.             value = source
  12.     return value
  13.  
  14. mylist = ['Hello', 123, 456.7]
  15.  
  16. while True:
  17.     response = input('Enter something (or just return to exit: ')
  18.     if not response:
  19.         break
  20.     value = find_type(response)
  21.     print(f'You entered {value} ({type(value).__name__}), which is '
  22.           f'{"" if value in mylist else "not "}in my list')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement