Advertisement
Adehumble

Week5 Coding Exercise 4

Feb 27th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. #4
  2. #This program is written to return the list of numbers with the greatest sum
  3.  
  4. #Function Program
  5. def greater_sum(expected_list1,expected_list2):
  6.     if sum(expected_list1)>sum(expected_list2):
  7.         print(expected_list1)
  8.     elif sum(expected_list2)>sum(expected_list1):
  9.         print(expected_list2)
  10.     else:
  11.         print("Both lists are equal")
  12.        
  13.  
  14. #Main Program
  15. user_list1=[]
  16. while True:
  17.     try:
  18.         n1=int(input("How many numbers do you want to play with in the first list? "))
  19.         break
  20.     except ValueError:
  21.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  22.         print("\n")
  23.        
  24. for m in range(n1):
  25.     while True:
  26.         try:
  27.             user_num1=int(input("Enter those numbers: "))
  28.             break
  29.         except ValueError:
  30.             print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  31.         print("\n")
  32.        
  33.     if user_num1==[]:
  34.         user_list1.append(0)
  35.    
  36.     user_list1.append(user_num1)
  37.    
  38.  
  39. user_list2=[]
  40. while True:
  41.     try:
  42.         n2=int(input("How many numbers do you want to play with in the second list? "))
  43.         break
  44.     except ValueError:
  45.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  46.         print("\n")
  47.        
  48. for n in range(n2):
  49.     while True:
  50.         try:
  51.             user_num2=int(input("Enter those numbers: "))
  52.             break
  53.         except ValueError:
  54.             print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  55.     user_list2.append(user_num2)
  56.    
  57. print("\n")
  58. print("Your first number list is: ", user_list1,"\nand your second number list is:  ", user_list2)
  59. print("Hence, The list with the highest sum is: ")
  60. greater_sum(user_list1,user_list2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement