Skip to content

Instantly share code, notes, and snippets.

@DragaDoncila
Created November 2, 2017 05:38
Show Gist options
  • Save DragaDoncila/15666a448fcac030d3bf00cc0443b9ce to your computer and use it in GitHub Desktop.
Save DragaDoncila/15666a448fcac030d3bf00cc0443b9ce to your computer and use it in GitHub Desktop.
"""
Script takes a target sum from the user, along with a list of integers, and displays the pairs of numbers in the list
which sum to the target given.
"""
target_sum = int(input("Enter target sum: "))
addends = []
user_input = input("Please enter a number (Q to stop): ").lower()
# get integers from user until they choose to stop (assume user enters either integers or q for simplicity)
while user_input != "q":
user_num = int(user_input)
addends.append(user_num)
user_input = input("Please enter a number (Q to stop): ").lower()
# for each number in the list
for i in range(len(addends)):
num_1 = addends[i]
# go through remaining numbers to the end of the list
for j in range(i+1, len(addends)):
num_2 = addends[j]
potential_sum = num_1 + num_2
if potential_sum == target_sum:
print("{} + {} = {}".format(num_1, num_2, target_sum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment