Created
November 2, 2017 05:38
-
-
Save DragaDoncila/15666a448fcac030d3bf00cc0443b9ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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