Advertisement
go6odn28

age_assignment

May 18th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. def age_assignment(*args, **kwargs):
  2.     person_info = {}
  3.     for name in args:
  4.         for letter in name:
  5.             if letter in kwargs:
  6.                 person_info[name] = kwargs[letter]
  7.  
  8.     sorted_person_info = dict(sorted(person_info.items()))
  9.     result = '\n'.join([f'{person} is {age} years old.' for person, age in sorted_person_info.items()])
  10.  
  11.     return result
  12.  
  13.  
  14. print(age_assignment("Peter", "George", G=26, P=19))
  15.  
  16. print(age_assignment("Amy", "Bill", "Willy", W=36, A=22, B=61))
  17.  
  18.  
  19. # COndition
  20. # Create a function called age_assignment() that receives a different number of names and a different number of key-value pairs. The key will be a single letter (the first letter of each name) and the value - a number (age). Find its first letter in the key-value pairs for each name and assign the age to the person's name.
  21. # Then, sort the names in ascending order (alphabetically) and return the information for each person on a new line in the format: "{name} is {age} years old."
  22.  
  23.  
  24. # print(age_assignment("Peter", "George", G=26, P=19))
  25. # OUTPUT
  26. # print(age_assignment("Peter", "George", G=26, P=19))  George is 26 years old.
  27. # Peter is 19 years old.
  28.  
  29. #print(age_assignment("Amy", "Bill", "Willy", W=36, A=22, B=61))
  30. # OUTPUT
  31. # Amy is 22 years old.
  32. # Bill is 61 years old.
  33. # Willy is 36 years old.
  34.  
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement