Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. ## Defining a input checker for integers
  2. def input_checker(list1):
  3. '''
  4. input: list
  5. return: Boolean
  6. Function that takes in a list and returns whether the list consists of only integers
  7. '''
  8.  
  9.  
  10. ## Elements in the list
  11. for i in list1:
  12. try:
  13. number=int(i)
  14.  
  15. ## Return False if value error
  16. except ValueError:
  17. return False
  18.  
  19. return True
  20.  
  21. ## The function to run the code
  22. def Num_list():
  23. ## Initialising the loop
  24. input_loop=True
  25.  
  26. while input_loop==True:
  27.  
  28. ## User input
  29. input_usr = input("Enter the integers seperated by spaces. Press X to exit: ")
  30.  
  31. ## Splitting the data by spaces
  32. input_usr = input_usr.split()
  33.  
  34. ## Exit code
  35. if input_usr[0].upper()=="X":
  36.  
  37. input_loop=False
  38.  
  39. ## Running the required function
  40. else:
  41.  
  42. ## Calling the input checker
  43. if input_checker(input_usr)==True:
  44.  
  45. ## Printing the user input
  46. print(input_usr)
  47. new_list=[]
  48.  
  49. ## Looping through the list
  50. for i in range(len(input_usr)):
  51. for j in range(i+1,len(input_usr)):
  52. ## Calculating the product and the sum of numbers in the list
  53. prod=int(input_usr[i])*int(input_usr[j])
  54. sum1=int(input_usr[i])+int(input_usr[j])
  55.  
  56. ## Appending the pairs in the list
  57. if prod%2==0 and sum1%2==1:
  58. new_list.append((input_usr[i],input_usr[j]))
  59.  
  60. ## Exit the loop
  61. input_loop=False
  62.  
  63. ## printing the required output
  64. print(new_list)
  65. else:
  66. print("Input is incorrect please try again")
  67. continue
  68.  
  69. ## Running the function
  70. if __name__ == '__main__':
  71. Num_list()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement