1
votes

I am trying to recreate a problem from our classwork:

Write a program that can handle a shopping event. First, it requests the number of items bought, then asks for each items' price and tax rate. Then prints the total cost.

Example:
How many items you bought: 2

For item 1
Enter the price: 10
Enter the tax rate: 0

For item 2
Enter the price: 20
Enter the tax rate: 8
Your total price is 31.6


I have no knowledge on how I would compute for items larger than 1. ie my code works for 1 item.
items = int(input("How many items did you buy? "))

for i in range(1, items+1, 1):
    print("For item ",i)
    price = float(input("Enter the price: "))
    tax_rate = float(input("Enter the tax rate: "))
    total = price + price*(tax_rate/100)

print("Your total price is", total)

I need to somehow save the totals after each iteration and add them all up. I am stumped.

Note: This is an introduction to python course and also my first programming course. We've only learned for loops thus far.

1
Use a list and .append() an item at every loop.iBug
Thanks @iBug. I saw this when I was trying to use google at first and even tried to use it myself, but unfortunately we have not learned how to append yet.CTG713

1 Answers

1
votes

You need to have an initialized counter to have a running total.

items = int(input("How many items did you buy? "))
total = 0

for i in range(1, items+1, 1):
    print("For item ",i)
    price = float(input("Enter the price: "))
    tax_rate = float(input("Enter the tax rate: "))
    total += price + price*(tax_rate/100)

print("Your total price is", total)