简体   繁体   中英

How to have Input display each day of the week?

I am trying to get the code to ask the user to enter sale on sunday thru saturday, while adding it to the list.

When I do so, it says, "'list' object cannot be interpreted as an integer" I'm not really too sure how to fix this with weekdays.

Thanks

    store_sales = []

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

    for sale in range(week_days):

        value = float(input('Enter sale amount for' + str(week_days) + ': '))

        store_sales.append(value)

        print(store_sales)

Can you please try with this code and let us know how it goes ?

store_sales = []

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

for sale in week_days:

    value = float(input('Enter sale amount for ' + sale + ': '))

    store_sales.append(value)

    print(store_sales)

Thanks

The error is showing because range takes either a single integer, or exactly three integers, and results in a sequence of integers with even spacing. Please see docs .

Like Gagan pointed out, you can just iterate through week_days directly, ie for sale in week_days .

You can also use the calendar module to avoid writing all the day names yourself.

import calendar
for day_name in calendar.day_name:
    value = ...

In your code, you just have to loop over week_days list and that's it.

store_sales = []
week_days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
for week_day in week_days:
    value = float(input('Enter sale amount for {} :'.format(week_day)))
    store_sales.append(value)
    print(store_sales)

The other simple way is to use calendar module :

import calendar
for week_day in calender.day_name:
    value = float(input('Enter sale amount for {} :'.format(week_day)))
    store_sales.append(value)
    print(store_sales)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM