Adehumble

Week5 Coding Exercise 2

Feb 27th, 2020
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. #2
  2. #This program is written to return the list odd numbers among the number choices of a user
  3.  
  4. print("METHOD1: Using For-Loop")
  5. #Function Program
  6. def only_odds(expected_list):
  7.     answer=[]
  8.     for num in expected_list:
  9.         if (num%2) != 0:
  10.             answer.append(num)
  11.     return(answer)
  12.  
  13. #Main Program
  14. user_list=[]
  15. while True:
  16.     try:
  17.         n=int(input("How many numbers would you like to play with? "))
  18.         break
  19.     except ValueError:
  20.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  21.         print("\n")
  22.        
  23. for i in range(n):
  24.     while True:
  25.         try:
  26.             user_num=int(input("Enter those numbers of your choice: "))
  27.             break
  28.         except ValueError:
  29.             print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  30.             print("\n")
  31.     user_list.append(user_num)
  32.    
  33. print("The odd numbers in", user_list,"are: ",only_odds(user_list))
  34.  
  35. print("\n")
  36.  
  37.  
  38.  
  39. print("METHOD 2: Using Filter Function")
  40. #Function Program
  41. def only_odds(expected_list):
  42.     return expected_list%2 !=0
  43.  
  44. #Main Program
  45. user_list=[]
  46. while True:
  47.     try:
  48.         n=int(input("How many numbers do u wish to play with: "))
  49.         break
  50.     except ValueError:
  51.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  52.         print("\n")
  53.        
  54. for i in range(n):
  55.     while True:
  56.         try:
  57.             user_num=int(input("Enter those numbers of your choice: "))
  58.             break
  59.         except ValueError:
  60.             print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  61.         print("\n")
  62.     user_list.append(user_num)
  63.  
  64. print("The odd numbers in", user_list,"are: ")
  65. print(list(filter(only_odds, user_list)))
  66.    
  67. print("\n")
  68.  
  69. print("METHOD 3: Using Lambda Function")
  70.  
  71. user_list=[]
  72. while True:
  73.     try:
  74.         n=int(input("How many numbers do u wish to play with: "))
  75.         break
  76.     except ValueError:
  77.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  78.         print("\n")
  79.        
  80. for i in range(n):
  81.     while True:
  82.         try:
  83.             user_num=int(input("Enter those numbers of your choice: "))
  84.             break
  85.         except ValueError:
  86.             print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  87.     user_list.append(user_num)
  88.  
  89. print("The odd numbers in", user_list,"are: ")
  90. print(list(filter(lambda user_no: (user_no%2) != 0,user_list)))
Advertisement
Add Comment
Please, Sign In to add comment