Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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? ")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading