Understanding the Python Algorithm for Adding Inputs

Asked 2 years ago, Updated 2 years ago, 49 views

In this way, what kind of program should I put together to add the input value?Please use Python as the language.

Input values:

200 
300
500
100
200

output:

200
500
1000
1100
1300

python algorithm

2022-09-30 16:48

2 Answers

The initial value is 0, and you should use a temporary variable that adds up every time you receive input."Since ""add in order"", note the ""value added to all the values entered so far"" in the middle."

A specific implementation would be to write a loop that repeats receiving and adding inputs with int(input()).


2022-09-30 16:48

While loop is sufficient, request and add a number.Stops the cycle only if the number entered is 0 and you can set another condition.

 num=0
while True:
    n = int( input("number:")
    if n == 0 : break # stop loop
    num+=n
    print(num)


2022-09-30 16:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.