Advertisement
Adehumble

Week6 Coding Exercise 3

Mar 10th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. #4
  2. #This program is written to return the values of a dictionary keys that are found in a particular list.
  3. print("This program will be making use of the names and ages of a list of students. The user will be prompted to provide the names of the students in which he/she needs the sum of their ages.\nThis program is very very smart. To mention a few of its intelligence, its not going to run into an error in case a user enters any foul student names.\nPlease, feel free to make use of this code in whatever length!\n")
  4.  
  5.  
  6.  
  7. #MAIN PROGRAM
  8. student_list=[]
  9. name_age_pair=[]
  10. student_dict={}
  11. Not_found_Names=[]
  12. Age_sum=0
  13. New_Names=[]
  14.  
  15. while True:
  16.     try:
  17.         number=abs(int(float(input("How many student names would you like to enter? "))))
  18.         break
  19.     except ValueError:
  20.         print("That is a wrong input. You must provide an integer. Please, try again!\n")
  21.  
  22. print("\n")
  23. for num in range(number):
  24.     while True:
  25.         try:
  26.             name,age=input("Please, enter the name and age of each student separated by a comma: ").strip().split(",")
  27.             print("\n")
  28.             name_age_pair=[name,abs(int(float((age))))]
  29.             break
  30.         except ValueError:
  31.             print("\nSomething is wrong with your inputs. Please, check for the following errors:\n\n1) Remember you have to enter just a pair of a name and age of each student at once.\n\n2) Only commas are required to separate your inputs.\n\n3) Make sure your second input is an integer.\n\nPlease try again!\n")
  32.        
  33.     student_list.append(name_age_pair)
  34.     student_dict=dict(student_list)
  35. print("Okay good! We are progressing.\n")
  36.  
  37. Names=input("Enter the names of the students you would like to sum their ages separated by a comma: ").strip().split(",")
  38.  
  39. print(f"\nThe following are the names of students you want to find the sum of their ages,{Names}\n")
  40.  
  41. for Name in Names:
  42.     if Name not in student_dict.keys():
  43.         Not_found_Names.append(Name)
  44.         continue
  45.        
  46.     else:
  47.         Age_sum = Age_sum + student_dict.get(Name)
  48.  
  49. for similar in Names:
  50.     if similar not in Not_found_Names:
  51.         New_Names.append(similar)
  52.        
  53. print(f"The following names: {Not_found_Names} are not in the list of names you entered.\n")
  54.  
  55. print("\nThe sum of the ages of ",New_Names,"is",Age_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement