Advertisement
gruntfutuk

strfloatint

Mar 15th, 2024
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. txt = input('Submission: ')  # give user a prompt
  2. lista = txt.replace(',', ' ').split()
  3. strt = ""
  4. flt = 0.0
  5. intt = 0
  6. seen_int = False
  7. kind = None
  8. for entry in lista:
  9.     if kind is None:
  10.         if entry in ('str', 'float', 'int'):
  11.             kind = entry  # what we expect next time
  12.         else:  # in case user mistyped
  13.             print(f'Unexpected word: {entry}')
  14.     else:  # kind should reference one of str, float or int from previous loop
  15.         if kind == "str":
  16.             strt += entry.strip()  # remove leading/trailing spaces
  17.         elif kind == "float":
  18.             flt += float(entry)  # place in try/except to catch errors
  19.         elif kind == "int":
  20.             if seen_int:
  21.                 intt *= int(entry)  # place in try/except to catch errors
  22.             else:  # don't want to multiple by 0 for first int
  23.                 intt = int(entry)  # place in try/except to catch errors
  24.                 seen_int = True
  25.         kind = None  # ready for next word
  26.  
  27. print(intt)
  28. print(flt)
  29. print(strt)
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement