Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Message to the user to enter a list of integers. The user input will be stored as a string of integers seperated by spaces
- input_string = input("Enter a list of integers separated by space. Your Input : ")
- #initializing counter flag to check for number of user attempts to provide input
- counter = 0
- #looping till the user provides the required input or runs out of attempts. After the initial mistake user is given 2 more chances
- while counter < 2 and not (input_string.replace(" ", "").isdigit() and len(input_string.replace(" ", "")) > 1):
- print("Wrong entry.")
- #inform user they have reached their last atempt.
- if counter == 1:
- msg = "Only enter integers seperated by space. Last chance. Your Input : "
- #remind user to only enter integers seperated by space
- else:
- msg = "Only enter integers seperated by space. Your Input : "
- #get user input as a string of integers seperated by space
- input_string = input(msg)
- counter = counter + 1
- #check if the input string contains only digits after removing the space. Proceed to calculation only if this satisfies
- if input_string.replace(" ", "").isdigit():
- #split input string by space and store it as a list
- input_list = input_string.split()
- #typecast list items into integers
- input_list = [int(i) for i in input_list]
- #initialize lists to store the calculation outputs
- prod_even = []
- sum_odd = []
- #looping over the range of input integer list, pair each element of the list with every other element in the list
- for i in range(len(input_list)):
- for j in range(i+1,len(input_list)):
- #check if a multiplied pair of integers is even
- if (input_list[i] * input_list[j])% 2 == 0:
- #append the satisfying pair to the product even list
- prod_even.append([input_list[i],input_list[j]])
- #check if the summed pair of integers is odd
- if (input_list[i] + input_list[j])% 2 != 0:
- #append the satisfying pair to the sum odd list
- sum_odd.append([input_list[i],input_list[j]])
- #print the output of the calculation for the user
- print("\nProduct Even pairs:")
- #print the pair of integers whose product is even
- for i in prod_even:
- print(str(i[0]) + "," + str(i[1]))
- print("\nSum Odd pairs:")
- #print the pair of integers whose sum is odd
- for i in sum_odd:
- print(str(i[0]) + "," + str(i[1]))
- else:
- print("Wrong entry.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement