0

I am trying to create a program which associates each day of the moth with a corresponding weekday. However, I am running into an issue when I attach the weekday.

January  = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for i in (January):
    for j in days:
        print("January", i,j)

When this is run, I get the following result:

January 1 Monday
January 1 Tuesday
January 1 Wednesday
January 1 Thursday
January 1 Friday
January 1 Saturday
January 1 Sunday
January 2 Monday
January 2 Tuesday
January 2 Wednesday
January 2 Thursday
January 2 Friday
January 2 Saturday
January 2 Sunday

This continues right up until 31.

Any help would be greatly appreciated, as I do not understand where I am going wrong.

Thank you! :)

timgeb
  • 76,762
  • 20
  • 123
  • 145
Veronica A
  • 11
  • 1

4 Answers4

2

With the nested for loops you are saying "for every day in January, iterate over days and print something`.

The word iterate is key here. You are going over the whole list days for every element in January.

What you are actually trying to do is to cycle the elements of days while iterating over the elements of January. Luckily, we have itertools.cycle for that!

Here's how I would implement your idea.

>>> from itertools import cycle
>>> 
>>> january = range(1, 32)
>>> days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
>>> 
>>> for day, dayname in zip(january, cycle(days)):
...     print('January {} {}'.format(day, dayname))
... 
January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
January 5 Friday
January 6 Saturday
January 7 Sunday
January 8 Monday
January 9 Tuesday
January 10 Wednesday
January 11 Thursday
January 12 Friday
January 13 Saturday
January 14 Sunday
January 15 Monday
January 16 Tuesday
January 17 Wednesday
January 18 Thursday
January 19 Friday
January 20 Saturday
January 21 Sunday
January 22 Monday
January 23 Tuesday
January 24 Wednesday
January 25 Thursday
January 26 Friday
January 27 Saturday
January 28 Sunday
January 29 Monday
January 30 Tuesday
January 31 Wednesday

If this is more than just a programming exercise, you might want to look into the datetime module in order to avoid manually computing the weekday of a given date.

timgeb
  • 76,762
  • 20
  • 123
  • 145
1

The second loop iterates over all 7 days of the week before going to the next day in the month.

One way to go about making what you need is by using a modulo operation:

January  = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for i in (January):
    # Need to subtract 1 because the loop variable i starts from 1 and not 0 
    current_day = (int(i) % 7) - 1
    print("January", i, days[current_day])

Output:

January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
January 5 Friday
January 6 Saturday
January 7 Sunday
January 8 Monday
January 9 Tuesday
January 10 Wednesday
January 11 Thursday
January 12 Friday
January 13 Saturday
January 14 Sunday
January 15 Monday
January 16 Tuesday
January 17 Wednesday
January 18 Thursday
January 19 Friday
January 20 Saturday
January 21 Sunday
January 22 Monday
January 23 Tuesday
January 24 Wednesday
January 25 Thursday
January 26 Friday
January 27 Saturday
January 28 Sunday
January 29 Monday
January 30 Tuesday
January 31 Wednesday
rdimaio
  • 325
  • 2
  • 3
  • 15
0

There are several issues with your code:

  • You need to cycle through your day strings ('Monday', 'Tuesday', etc) repeatedly. You haven't specified any logic to achieve this. One solution (see below) is itertools.cycle.
  • You don't need to create a list of string numbers. You don't need a list, or even strings. Just use a range object to specify a range.
  • Use zip to iterate sequentially an iterable of day numbers and strings. You then only need a single for loop.
  • You can use f-strings (Python 3.6+) as an efficient and readable alternative to manual string concatenation.

Here's a working example:

from itertools import cycle

January_days = range(1, 32)

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for day_num, day_str in zip(January_days, cycle(days)):
    print(f'January {day_num} {day_str}')

January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
...
January 27 Saturday
January 28 Sunday
January 29 Monday
January 31 Wednesday
jpp
  • 159,742
  • 34
  • 281
  • 339
0
for i in (January):
for j in days:
    print("January", i,j)

This is

for i in ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] :
    for j in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]:
    print("January", i, j)

In first Iteration of outer loop

i = "1"
j = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

In first Iteration of outer loop

i = "2"
j = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

....

Hence the output above.

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91