Posts: 13
Threads: 3
Joined: Mar 2021
Mar-22-2021, 06:39 PM
(This post was last modified: Mar-22-2021, 06:45 PM by Larz60+.
Edit Reason: added inline code tags
)
Sorry if this is stupid but I have created a list of days
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
I am trying to assign the weekdays to an empty list:
weekdays = []
and the weekend days to an empty list:
weekends = []
I can't figure out how to assign the days properly, would I use a while loops or a for loop? Or would I need to loop them at all?
Sorry again if it's a stupid question.
Posts: 1,025
Threads: 16
Joined: Dec 2016
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = days[:5]
weekends = days[5:]
print(weekdays)
print(weekends) Output: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
['Saturday', 'Sunday']
Posts: 13
Threads: 3
Joined: Mar 2021
(Mar-22-2021, 06:47 PM)Axel_Erfurt Wrote: days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = days[:5]
weekends = days[5:]
print(weekdays)
print(weekends) Output: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
['Saturday', 'Sunday']
Hey Axel,
Thanks so much but does this mean I'm assigning the days to empty lists or am I just printing the first five and then anything after the first five?
Thanks again
Posts: 1,025
Threads: 16
Joined: Dec 2016
They are not empty
weekdays is a new list with the week days
weekends is a new list with the weekend days
Posts: 13
Threads: 3
Joined: Mar 2021
(Mar-22-2021, 06:52 PM)Axel_Erfurt Wrote: They are not empty
weekdays is a new list with the week days
weekends is a new list with the weekend days
I see thank you, do you know a way to assign the days to two separate empty lists?
Posts: 12,022
Threads: 484
Joined: Sep 2016
Hint:
>>> import calendar
>>> for num, day in enumerate(calendar.day_name):
... print(num, day)
...
0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
>>>
Posts: 13
Threads: 3
Joined: Mar 2021
(Mar-22-2021, 06:59 PM)Larz60+ Wrote: Hint:
>>> import calendar
>>> for num, day in enumerate(calendar.day_name):
... print(num, day)
...
0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
>>>
Hi, Thanks so much for getting back to me.
Would I be right in thinking that I would need to print number 0-4 and assign them to the first empty list and then assign the 5-6 to the second empty list?
It's how to assign it that I'm mainly having trouble with.
Sorry :(
Posts: 13
Threads: 3
Joined: Mar 2021
Mar-22-2021, 07:18 PM
(This post was last modified: Mar-23-2021, 12:17 AM by Larz60+.)
Would something like this work:
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
#Create empty lists for weekdays and weekends
weekdays = []
weekends = []
index = 0
while index < len(days):
day = days[index]
print( days )
index = index + 1
Larz60+ write Mar-23-2021, 12:17 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Posts: 1,025
Threads: 16
Joined: Dec 2016
You have a list with all days and you want 2 lists with weekdays and weekend days ?
I wrote it before
weekdays = days[:5]
weekends = days[5:]
Posts: 13
Threads: 3
Joined: Mar 2021
(Mar-22-2021, 07:40 PM)Axel_Erfurt Wrote: You have a list with all days and you want 2 lists with weekdays and weekend days ?
I wrote it before
weekdays = days[:5]
weekends = days[5:]
Thank you again for replying, this is what I'm trying to do:
Create a list containing the days of the week from Monday to Sunday and two empty lists weekday = [ ] and weekend = [ ]. Allocate each element from the list containing the days to the appropriate list: either weekday or weekend.
Print the weekdays from the weekday list in the order from Monday to Friday.
Print the weekend days from the weekend list in the order Saturday, Sunday.
|