# display month and days using for loops

• (2 Pages)
• 1
• 2

## 24 Replies - 2533 Views - Last Post: 02 July 2013 - 06:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323836&amp;s=9917536607a377f13c8c79b659d24629&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

# display month and days using for loops

Posted 28 June 2013 - 09:49 AM

I am trying to display each month of the year and the number of days for each month. My code is running 12 times and not sure how to make it run only once.

```month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
days_Month =[31,28,31,30,31,30,31,31,30,31,30,31]

for index in range(len(month)):
for index in range(len(days_Month)):
print '%s %3d' (month[index],days_Month[index])

```

Is This A Good Question/Topic? 0

## Replies To: display month and days using for loops

### #2 baavgai

• Dreaming Coder

Reputation: 6053
• Posts: 13,107
• Joined: 16-October 07

## Re: display month and days using for loops

Posted 28 June 2013 - 10:01 AM

Consider:
```for x in range(12):
for y in range(12):
print (x,y)

```

What do you expect from this?

### #3 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 10:08 AM

I tried that, it prints out each number 12 times for each for loop.
(1,1)
(1,2)
(1,3)
etc...

### #4 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: display month and days using for loops

Posted 28 June 2013 - 10:20 AM

Quote

I tried that, it prints out each number 12 times for each for loop.

Right. What you want is to print 12 lines, not 144 lines. So if you have a loop that goes around 12 times, and you do that loop 12 times, that's not what you're looking for.

Think about it in English: for each month, print the number of days in that month.

What you're doing now is, for each month, for each "number of days", print that "number of days".

Python offers a few ways to do this. The simplest is the one you're almost at.

Once you've solved it with a simple loop over a range, you might like to look up the "zip" function and see if you can see how you'd use that to solve this - but do that AFTER you've solved it with the loop!

This post has been edited by jon.kiparsky: 28 June 2013 - 10:24 AM

### #5 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 10:29 AM

Not quite sure I understand what you are saying.

I understand that if I use:
```for i in month:

```

it will iterate through the list once and display a list of my months.
I am running into trouble trying to understand why my second loop doesn't do the same thing.

### #6 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: display month and days using for loops

Posted 28 June 2013 - 10:36 AM

It does do the same thing. It does the same thing for the first i in month, and then it does it again for the second i in month, and so forth, twelve times.

### #7 baavgai

• Dreaming Coder

Reputation: 6053
• Posts: 13,107
• Joined: 16-October 07

## Re: display month and days using for loops

Posted 28 June 2013 - 11:42 AM

Think about it. Don't you just want to call:
```print month[0], days_Month[0]
print month[1], days_Month[1]
...
print month[11], days_Month[11]

```

What kind of loop would make that happen?

### #8 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 11:56 AM

See there lies the problem.

we were given an example loop by instructor (no book for class)
example was
```for i in range (1,13):
for j in range (1,32):
for k in range(1,6):
print"%10d%10d%10d ______________"%(ijk)

```

I'm attempting to write my code from this example and have to use 2 for loops.

### #9 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: display month and days using for loops

Posted 28 June 2013 - 12:00 PM

You don't want two for loops here. You want to output something like

January 31
February 28
March 31
...
November 30
December 31

Let's take it in pieces. First, how you you print just the months?

Then, how do you print just the number_of_days? (31, 28, 31...30, 31)

Then, how do you print just
January 31

(by reference to the two lists you've set up, I mean)

Finally, how do you get the desired output?

### #10 woooee

Reputation: 27
• Posts: 109
• Joined: 21-November 12

## Re: display month and days using for loops

Posted 28 June 2013 - 12:20 PM

Get another instructor, or you misunderstood. First, i, j, and k are not descriptive enough to tell what is going on, especially
for k in range(1,6):??
You are being taught bad habits. Usually you would be looking up many times so you would create one list that only requires one lookup.
```months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
days_Month =[31,28,31,30,31,30,31,31,30,31,30,31]
##---------------- first loop ---------------
## combine into one list
month_and_days = []
for ctr in range(len(months)):
month_and_days.append([months[ctr], days_Month[ctr]])
print month_and_days
print

##---------------- 2nd loop ---------------
for mth, days in month_and_days:
print mth, days

print
for mth in [1, 6, 10]:
abbrev, days = month_and_days[mth-1]
print "month #%d is %s and has %d days" % (mth, abbrev, days)
```

This post has been edited by woooee: 28 June 2013 - 12:29 PM

### #11 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 12:30 PM

To print just the months I would
```
for i in range(12):
print month[i]

```

or this
```for i in month:
print i

```

To print the days of the month I would do it the same way. Both of those loops will print out each item in my list. I also realize that the second loop is an inner loop which will iterate through the first loop 12 times giving me 144 lines with each month display each day from second list.
I'm using two loops because those are the instructions, otherwise I would try something different.

Using an array for names of the months and another for how many days in each month, (ignore leap year if you want). And using 2 for loops with ranges based on the month and how many days are in the month, print out a one line per day calendar.

The output MUST use a string with formatting built into it, one for the string, and one for the day of the month. Make the day of the month be 3 digits wide.

e.g.

monthNames = [ "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]

daysInMonth = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]

Am I misunderstanding the instructions?

### #12 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: display month and days using for loops

Posted 28 June 2013 - 12:50 PM

Your instructions are wrong. Talk to your instructor if you want, but there is no reason to want to do this with two loops.

The only way you could do this with two loops would be something like

```for i in range (len(month)):
for j in range (len(days_Month)):
if j == i:
print month[i], days_Month[j]

```

But this is so awful it makes me want to vomit just typing it in.

I guess you could just do something like

```for i in range (len(month)):
for j in range (len(days_Month)):
pass
print month[i], days_Month[i]

```

if you really think you want another loop in there, but it's completely pointless

dannii, on 28 June 2013 - 02:30 PM, said:

And using 2 for loops with ranges based on the month and how many days are in the month, print out a one line per day calendar.

Aha! Yes, I think you've misunderstood the assignment. It's not to print out the number of days in each month at all.

Yes, this does require two loops.

This post has been edited by jon.kiparsky: 28 June 2013 - 12:50 PM

### #13 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 12:55 PM

I'm very frustrated, It is difficult to learn without a book and trying to learn by googling isn't the best way(actually it sucks). I can not get another instructor since this is the only instructor for this class. Class was informed that instructions a vague due to real life. Instructions are vague in real life employment. Now that I am re-reading instructions I'm not sure if I am to print out all days of the month or just the month and how many days are in the month.

### #14 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: display month and days using for loops

Posted 28 June 2013 - 12:58 PM

The line I quoted looks very clear to me. A one line per day calendar means a calendar, with one line for each day.

### #15 dannii

Reputation: 0
• Posts: 44
• Joined: 28-June 13

## Re: display month and days using for loops

Posted 28 June 2013 - 01:00 PM

Okay I see that now. Now to look up how to do that.