Advertisement
Adehumble

Week6 Coding Exercise 4

Mar 8th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #4
  2. print("This program is written to return a list of items that exist as both a key and a value in a dictionary.\n")
  3.  
  4.  
  5. #FUNCTION PROGRAM
  6. def common_elements(expected_dict):
  7.     common_elements=[]
  8.     for key, value in expected_dict.items():
  9.         if key in expected_dict.keys() and key in expected_dict.values():
  10.             common_elements.append(key)
  11.        
  12.     print(f"Your dictionary is: {expected_dict}.\n\nThe common elements in your dictionary are: {common_elements}")
  13.    
  14.            
  15. #MAIN PROGRAM
  16. pair_list=[]
  17. user_list=[]
  18. user_dict={}
  19.  
  20. while True:
  21.     try:
  22.         number=abs(int(float(input("How many key-value pairs would you like to enter? "))))
  23.         break
  24.     except ValueError:
  25.         print("That is a wrong input. You must provide an integer. Please, try again!\n")
  26.  
  27. print("\n")
  28. for num in range(number):
  29.     while True:
  30.         try:
  31.             input1,input2 = input("\nEnter each key-value pair separated by a comma: ").split(",")
  32.             break
  33.         except ValueError:
  34.             print("Something is wrong with your inputs.\n\nRemember you have to enter just a pair of key and value at once, separated by a comma.\nAlso, no spaces are allow. Please try again!\n")
  35.            
  36.    
  37.     pair_list=[input1,input2]
  38.     user_list.append(pair_list)
  39. user_dict=dict(user_list)
  40.  
  41. print("\n")
  42. common_elements(user_dict)
  43.  
  44. print("\n")
  45. repeat_factor=input("Would you like to run the program again?\nPlease, select either option A or B\nA) Yes\nB) No        ").upper()
  46.    
  47. if repeat_factor=="A":
  48.     pair_list=[]
  49.     user_list=[]
  50.     user_dict={}
  51.            
  52.     while True:
  53.         try:
  54.             number=abs(int(float(input("How many key-value pairs would you like to enter? "))))
  55.             break
  56.         except ValueError:
  57.             print("That is a wrong input. You must provide an integer. Please, try again!\n")
  58.                
  59.     for num in range(number):
  60.         while True:
  61.             try:
  62.                 input1,input2 = input("\nEnter each key-value pair separated by a comma: ").split(",")
  63.                 break
  64.             except ValueError:
  65.                 print("Something is wrong with your inputs.\n\nRemember you have to enter just a pair of key and value at once, separated by a comma.\nAlso, no spaces are allow. Please try again!\n")
  66.                
  67.         pair_list=[input1,input2]
  68.         user_list.append(pair_list)
  69.     user_dict=dict(user_list)
  70.            
  71.     print("\n")
  72.     common_elements(user_dict)
  73.        
  74. elif repeat_factor=="B":
  75.         print("Thank your for using this program. See you next time!")
  76.        
  77. else:
  78.     print("You entered an incorrect option. Your choices are only limited to A or B. Bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement