Target Sum Accumulator

Write a Python program that first asks the user for a target integer t. Then the program should continually ask the user to input numbers until the sum of the input numbers equals the target number t.

Example 1:

Input: t = 10, numbers = 2, 3, 5 

Output: "Target achieved"

Example 2:

Input: t = 6, numbers = 1, 1, 1, 1, 1, 1 

Output: "Target achieved"

Start with a variable sum initialized to 0. Inside a while loop, keep adding the input numbers to sum. The loop should continue as long as sum is less than t.

t = int(input("Enter the target sum: "))
sum = 0
while sum < t:
    num = int(input("Enter a number: "))
    sum += num
print("Target achieved")

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!