Home > other >  account deleted account deleted account deleted
account deleted account deleted account deleted

Time:04-08

account deleted account deleted account deleted

CodePudding user response:

'Process finished with exit code 0' merely means that your program compiled and ran successfully without any errors. The reason you're not seeing anything is because nothing is actually being run since all your statements are definitions. What you need to do is run the function so that the print statement executes. Try adding something like this to the end of your code and then run it again:

two_number_sum(my_numbers, target_sum)

Also, you may want to consider changing the parameter names in your function definition so they are not confused with the variables you have defined. Parameter names do not affect the parameters themselves, which are what you pass in when you actually call the function.

If you didn't intend to write this as a function, you can also just remove the function definition and run your code as:

my_numbers = [-2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
target_sum = 20

for x in range(len(my_numbers)-1):
    for y in range(x 1, len(my_numbers)):
        if my_numbers[x]   my_numbers[y] == target_sum:
            print(my_numbers[x], my_numbers[y])

CodePudding user response:

This slightly reorganizes your code to meet your spec:

my_numbers = [-2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
target_sum = 20

def two_number_sum(my_numbers, target_sum):
    winners = []
    for x in range(len(my_numbers)-1):
        for y in range(x 1, len(my_numbers)):
            if my_numbers[x]   my_numbers[y] == target_sum:
                winners.append( (my_numbers[x], my_numbers[y]) )
    return winners

print(two_number_sum(my_numbers, target_sum))
  • Related