2

This is the question from the Python book:

Design a program that asks the user to enter a store's sales for each day of the week. The amounts should be stored in a list. Use a loop to calculate the total sales for the week and display the result.

This is what I have so far in the Python code:

Sunday = int(input("Enter the store sales for Sunday: "))
Monday = int(input("Enter the store sales for Monday: "))
Tuesday = int(input("Enter the store sales for Tuesday: "))
Wednsday = int(input("Enter the store sales for Wednsday: "))
Thursday = int(input("Enter the store sales for Thursday: "))
Friday = int(input("Enter the store sales for Friday: "))
Saturday = int(input("Enter the store sales for Saturday: "))

store_week_sales = [Sunday, Monday, Tuesday, Wednsday, Thursday, Friday, Saturday]

index = 0

I'm not so sure on how to add the loop so that I can calulate the total sales of the week. Help would be greatly appreciated.

1
  • Something not directly related to the question: You could do sales = [int(input('Enter store sales for %s: ' % day)) for day in ['Sunday', 'Monday', 'Tuesday', 'Wednesday']] Commented Dec 1, 2014 at 17:55

3 Answers 3

3

Try this:

total = 0

for store_sale in store_week_sales:
    total += store_sale

print "Total week sales: %.2f" % total

Python does not makes a difference between for and (non-existent) foreach, since for already iterates over the elements of an iterable, not over index numbers.

Sign up to request clarification or add additional context in comments.

Comments

1

If you absolutely want to do it with a for loop, it can be done like heltonbiker describes. Alternatively, you can do it with the function sum.

sumOfList = sum(store_week_sales);

Since it is an exercise in for loops, this is probably not what your looking for this time, but it can be good to know for future reference.

Comments

0
def main():

    total = 0.0
    daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    index = 0

    days_of_the_week = ['Sunday', 'Monday', 'Tuesday', 'Wednsday', 'Thursday', 'Friday', 'Saturday']

    for index in range(7):

        print("Enter the amount of sales for", days_of_the_week[index])
        daily_sales[index] = float(input("Enter the sales here: "))

        total += daily_sales[index]

    print("The total sales for the week is $", format(total, '.2f'), sep = ' ')

main()

1 Comment

Please add some narrative to explain what this code you dumped on us does and how/why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.