Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_type(source):
- 'function to examine string and determine if it contains'
- 'a viable float, integer or string and return appropriate'
- 'type accordingly'
- try:
- value = None
- value = float(source) # try to convert source to float
- value = int(source) # float worked, let's try integer
- except ValueError: # not float or integer
- if not value: # cast to float failed so neither float nor string
- value = source
- return value
- mylist = ['Hello', 123, 456.7]
- while True:
- response = input('Enter something (or just return to exit: ')
- if not response:
- break
- value = find_type(response)
- print(f'You entered {value} ({type(value).__name__}), which is '
- f'{"" if value in mylist else "not "}in my list')
Add Comment
Please, Sign In to add comment