Advertisement
Souvik_Chaki

Bootcamp Programming Python

Jul 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #Message to the user to enter a list of integers. The user input will be stored as a string of integers seperated by spaces
  2. input_string = input("Enter a list of integers separated by space. Your Input : ")
  3. #initializing counter flag to check for number of user attempts to provide input
  4. counter = 0
  5. #looping till the user provides the required input or runs out of attempts. After the initial mistake user is given 2 more chances
  6. while counter < 2 and not (input_string.replace(" ", "").isdigit() and len(input_string.replace(" ", "")) > 1):
  7.     print("Wrong entry.")
  8.     #inform user they have reached their last atempt.
  9.     if counter == 1:
  10.         msg = "Only enter integers seperated by space. Last chance. Your Input : "
  11.     #remind user to only enter integers seperated by space
  12.     else:
  13.         msg = "Only enter integers seperated by space. Your Input : "
  14.     #get user input as a string of integers seperated by space
  15.     input_string = input(msg)
  16.     counter = counter + 1
  17. #check if the input string contains only digits after removing the space. Proceed to calculation only if this satisfies
  18. if input_string.replace(" ", "").isdigit():
  19.     #split input string by space and store it as a list
  20.     input_list = input_string.split()
  21.     #typecast list items into integers
  22.     input_list = [int(i) for i in input_list]
  23.     #initialize lists to store the calculation outputs
  24.     prod_even = []
  25.     sum_odd = []
  26.     #looping over the range of input integer list, pair each element of the list with every other element in the list
  27.     for i in range(len(input_list)):
  28.         for j in range(i+1,len(input_list)):
  29.             #check if a multiplied pair of integers is even
  30.             if (input_list[i] * input_list[j])% 2 == 0:
  31.                 #append the satisfying pair to the product even list
  32.                 prod_even.append([input_list[i],input_list[j]])
  33.             #check if the summed pair of integers is odd
  34.             if (input_list[i] + input_list[j])% 2 != 0:
  35.                 #append the satisfying pair to the sum odd list
  36.                 sum_odd.append([input_list[i],input_list[j]])
  37.     #print the output of the calculation for the user
  38.     print("\nProduct Even pairs:")
  39.     #print the pair of integers whose product is even
  40.     for i in prod_even:
  41.         print(str(i[0]) + "," + str(i[1]))
  42.     print("\nSum Odd pairs:")
  43.     #print the pair of integers whose sum is odd
  44.     for i in sum_odd:
  45.         print(str(i[0]) + "," + str(i[1]))
  46. else:
  47.     print("Wrong entry.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement