Advertisement
Adehumble

Week6 Coding Exercise 2

Mar 8th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. #2
  2. print("This program is written to return a dictionary where the keys represent a length and the values represent how many strings have that length \n")
  3.  
  4. #FUNCTION PROGRAM
  5. def length_count(userword_list):
  6.     key_value_pair=[]
  7.     dict_list=[]
  8.     answer=[]
  9.    
  10.    
  11.     for userword in userword_list:
  12.         frequency=userword_list.count(userword)
  13.         key_value_pair=[len(userword), frequency]
  14.         if key_value_pair in dict_list:
  15.             key_value_pair=[len(userword),frequency+1] 
  16.         dict_list.append(key_value_pair)
  17.    
  18. #The lines directly below will remove repetitions of each key_value pair
  19.        
  20.         for key_value in dict_list:
  21.             if key_value not in answer:
  22.                 answer.append(key_value)
  23.    
  24.        
  25.     print(f"Your word list is {userword_list} and your answer is: \n\n{dict(answer)}")
  26.  
  27. #MAIN PROGRAM
  28. word_list=[]
  29.  
  30. while True:
  31.     try:
  32.         num=abs(int(float(input("How many numbers do you want to play with? "))))
  33.         break
  34.     except ValueError:
  35.         print("That is a wrong input. Make sure you enter a number. Please, try again!\n")
  36.  
  37. print("\n")
  38. for n in range(num):
  39.     word=input("Please, enter your word choices one after the other: ")
  40.     word_list.append(word)
  41.    
  42.  
  43. print("\n")
  44. length_count(word_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement