Home > Back-end >  sum until a value is reached
sum until a value is reached

Time:10-09

I want to take user inputs from 3 values(Let's say 2, 5, 10) and add them until a particular value is reached (Example 100) and stop the process. I'm having trouble choosing whether the for loop is to be used or if-else statements or while break statements.

CodePudding user response:

I think you might want this:

def special_sum(values=[2, 5, 10], maximum=100):
    result = 0

    for value in in values:
        if result > maximum:
            break
        result  = value

    return result

CodePudding user response:

if you want to add them over and over again until its greater than or equal to 100 then you can do this -

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))

sum = a b c

while(True):
    if (sum sum)>=100:
        break
    
    sum = sum sum
    print(sum)
  • Related