I was wondering how to have a price total in this code.
As you can see, the code repeats as is necessary so I was wondering how to add up a total if there is only on price variable.

times = int(input('How many items on your shopping list? '))
def question(times):
grocery_list = {}
print
print ('Enter grocery item and price.')
print
for i in range(times):
    key = input('Item %d Name: ' % int(i+1))
    value = input('Item %d Price: $' % int(i+1))
    print
    grocery_list[key] = value
print (grocery_list)
question(times)

Recommended Answers

All 2 Replies

Also, I am NOT asking for you guys to do this project for me. I just wanted to understand how to make a total if I was using a vairable that was reloaded as many times as the user wanted.

You need another variable to hold the total price. Set it to zero before entering the loop. In the loop add each result to the total price. After the loop you can print it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.