getting the sum of multiple inputs under the same variable

while True:
    price = int(input("please enter the price for the product: $"))
    a = input("Do you want to enter another product? (Enter y for yes)")
    if a == "y":
        continue
    elif a == "n":
        total= sum(price)
        print("the total amount for the products is:  $", total)
        
    else:
        print("the input is invalid, please try again (y for yes, n for no) ")
        a = input("Do you want to enter another product? ")

to calculate the sum of the price what should I do I tried using the sum command but it caused an error in my code.
the error I got is Unexpected type(s):(int)Possible type(s):(Iterable)(Iterable)

>Solution :

Pretty simple, actually. Initialize the total variable with 0 outside the loop, and keep on adding price to total.

total = 0
while True:
    price = int(input("please enter the price for the product: $"))
    total = total + price
    a = input("Do you want to enter another product? (Enter y for yes)")
    if a == "y":  
        continue
    elif a == "n":
        print("the total amount for the products is:  $", total)
        
    else:
        print("the input is invalid, please try again (y for yes, n for no) ")
        a = input("Do you want to enter another product? ")

Leave a Reply