Advertisement
simeonshopov

Extract Person Information

Feb 14th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. n = int(input())
  2.  
  3. def found_name_age(a: str, b: str):
  4.     if a == '@' or a == '#':
  5.         num = ''
  6.         idx = b.index(a) + 1
  7.         while True:
  8.             if b[idx] == '|' or b[idx] == '*':
  9.                 break
  10.             num += b[idx]
  11.             idx += 1
  12.         return num
  13.  
  14.  
  15. for _ in range(n):
  16.     text = input()
  17.     person = []
  18.     i = 0
  19.     while i < len(text) - 1:
  20.         if text[i] == '@':
  21.             name = found_name_age(text[i], text)
  22.             person.append(name)
  23.             i += len(name)
  24.         elif text[i] == '#':
  25.             age = int(found_name_age(text[i], text))
  26.             person.append(age)
  27.             i += len(str(age))
  28.         if len(person) == 2:
  29.             if str(person[1]).isdigit():
  30.                 print(f'{person[0]} is {person[1]} years old.')
  31.                 name = ''
  32.                 age = -111
  33.                 person = []
  34.             else:
  35.                 print(f'{person[1]} is {person[0]} years old.')
  36.                 name = ''
  37.                 age = -111
  38.                 person = []
  39.         i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement