Hello again, I am working on a homework problem and I am not able to get the accumulator to accumulate the tickets sales for each section.

My assignment is to create a basic program that will use loops. I need to have it ask for each day of the week how many tickets were sold in each category. Also I need it to accumulate the total were sold throughout the week within each category. I played around with what I have now and it seems to only accumulate what the last day is not each day. Can someone help me figure out what I am doing wrong? Also if you have any tips for a better way of coding it I am trying to learn as much as possible. (please understand this is a fundamentals class though :P )

``````def main():
#Seat pricing per section
classA = 15
classB = 12
classC = 9
#Number of days
week = 7

#Loop to ask for and recieve user input for how many sold in each section
for sales_week in range (week):

totalA = 0
totalB = 0
totalC = 0

print('For day', sales_week+1)
print('---------')
ticketA = int(input('Enter the number of Class A seats were sold: '))
ticketB = int(input('Enter the number of Class B seats were sold: '))
ticketC = int(input('Enter the number of Class C seats were sold: '))
print('')
totalA += ticketA
totalB += ticketB
totalC += ticketC

seatsA(classA, totalA)
seatsB(classB, totalB)
seatsC(classC, totalC)

grand_total = totalA + totalB + totalC

print('The total of all seats sold is: ', grand_total)

#Fuctions to calculate the totals
def seatsA(price, seats):
classA_Total = price * seats
print('\$', classA_Total, ' in Class A seats were sold.', sep='')

def seatsB(price, seats):
classB_Total = price * seats
print('\$', classB_Total, ' in Class B seats were sold.', sep='')

def seatsC(price, seats):
classC_Total = price * seats
print('\$', classC_Total, ' in Class C seats were sold.', sep='')

main()
``````

## Recommended Answers

What happens to total variables when for loop advances to next iteration?

## All 4 Replies

What happens to total variables when for loop advances to next iteration?

Do I need to add a nested loop and have them within the inner loop?

Here is an example of how it should be done:

``````# initial value
total_sold = 0

for x in range(3):
sold = int(input("Enter number of items sold: "))
total_sold += sold
print(total_sold)  # test
``````

If you put the initial value in the loop, it would continually reset.

Ok so that did work when I took the initial total values out and put them before the loop. Can you explain why so I understand it fully.

Thank you once again Dandiweb Community!!

Be a part of the DaniWeb community

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