[Solved] What is wrong with this code? I cant figure it out


days_of_week = ['Sunday', 'Monday', 'Tuesday',
                'Wednesday', 'Thursday', 'Friday',
                'Saturday']

You forget the , after the ‘Thursday’, that’s why it will out of range.


your code:

def main():
    # Variables
    total_sales = 0.0

    # Initialize lists
    daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    days_of_week = ['Sunday', 'Monday', 'Tuesday',
                    'Wednesday', 'Thursday', 'Friday',
                    'Saturday']

    for i in range(7):
        daily_sales[i] = float(input('Enter the sales for ' \
                                 + days_of_week[i] + ': '))

    for number in daily_sales:
        total_sales += number

    # Display total sales
    print('Total sales for the week: ${:.2f}'.format(total_sales))

# Call the main function.
main()

And maybe we can improve this code a bit:

def main():
    # Initialize lists
    daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
                    'Friday', 'Saturday']

    # Use `enumerate` to get the item directly.
    for index, day in enumerate(days_of_week):
        daily_sales[index] = float(
            input('Enter the sales for {0}: '.format(day)))

    # Use the built-in `sum` to sum the sales.
    total_sales = sum(daily_sales)

    # Display total sales
    print('Total sales for the week: ${:.2f}'.format(total_sales))


# Call the main function.
main()

4

solved What is wrong with this code? I cant figure it out