display month and days using for loops

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1756 Views - Last Post: 02 July 2013 - 06:43 AM Rate Topic: -----

#16 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 28 June 2013 - 04:50 PM

I am able to print out all the months, now my second for loop doesn't iterate past the first number in my list.
i = 0
for i in range(1,(days_Month[i])):



I haven't found anything to explain why.
Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7892
  • View blog
  • Posts: 13,421
  • Joined: 19-March 11

Re: display month and days using for loops

Posted 28 June 2013 - 05:03 PM

What is the value of range(1, (days_Month[1]))?

Don't guess at this: start up the interpreter, load up your source file, and examine the value for yourself. Use the -i option to start the interpreter and include a source file. Like so:

$ python -i mysourcefile.py


From there, you can examine values and test hypotheses. For example:

>> range(1,(days_Month[1]))

days_Month[1] being a useful value to examine, since that'll be the first one your loop sees.

(This won't be the source of your problem, but why is days_Month[i] in parentheses? Also, why do you declare a value for i, and then use it as a loop index?)
Was This Post Helpful? 0
  • +
  • -

#18 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 28 June 2013 - 05:22 PM

range of values is (start, end)
so the 1 is count will start at 1
Jan 1
Jan 2
Jan 3
...
...
Dec 28
Dec 29
Dec 30

The end count should come from my days_Month(31,28,...30,31)
However when I use days_month without the i I get an error,
Was This Post Helpful? 0
  • +
  • -

#19 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 28 June 2013 - 05:31 PM

I see that i = 0 is the first number (31) in list i = 1 is the second number(28) etc... I also thought that if I used 1 += 1 it would add one through each iteration, but when I try this it causes an error.
Was This Post Helpful? 0
  • +
  • -

#20 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: display month and days using for loops

Posted 29 June 2013 - 06:34 AM

View Postdannii, on 28 June 2013 - 09:49 AM, said:

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]))
    break



Hello Dannii,

Your program is perfectly fine, you just made a small error. You forgot to add a percent sign % at line 6:
print ('%s %3d' % (month[index],days_Month[index]))


In addition, an addition tip make sure to add brackets when you are printing because in Python 3.x, print() is no longer a statement but a function.

Edit:
My bad your program has run 144x, repeating twelve times. add a break to the i-loop so that the program only run 12x

(0,0), (0,1), (0,2), (0,3),(0,4), (0,5), (0,6), (0,7), (0,8), (0, 9), (0,10), (0,11), (0,12)

You don't want your i-loop to iterate through one more time. Or else you would get:

(1,0), (1,1)..., (1,12)

Meaning that your program will run one more time.

H Hopefully that made some sense.

This post has been edited by chan 06: 29 June 2013 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#21 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 29 June 2013 - 07:28 AM

Thank you Chan 06, I understand the code, I misunderstood the assignment so now I'm working on the second loop. I get that the days in the days_Month are the ending days and I need to use a for loop that counts from 1 to the end day in the days_Month list. I've not been anything online to explain how to do it.
Was This Post Helpful? 0
  • +
  • -

#22 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7892
  • View blog
  • Posts: 13,421
  • Joined: 19-March 11

Re: display month and days using for loops

Posted 29 June 2013 - 08:33 AM

What is it that's not working now? Perhaps you should post your current code and an example of what it's doing, and why that's not what you want.
Was This Post Helpful? 0
  • +
  • -

#23 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 29 June 2013 - 09:09 AM

I'm going to post code first then ask questions.
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]

for month in Months:
    for days in range{1,32):
    print "%s %3d" %(month, days)



This code will print out the month and all days from 1 to 31 I understand it.
if I substitute
for days in days_Month:
    print "%s %3d" %(month, days)



It will print out:
Jan 31
Jan 28
Jan 31
...
...
...
Dec 31
Dec 30
Dec 31
And I understand that also it is displaying the months with each day in the days_month list.

I realize that I need to iterate through the days_month list once. the first number 31 goes with Jan in the first list, and I need to display Jan 1 through Jan 31 before moving to the second number in the list which is 28.
If I try to use days_month I get an error integer end argument expected, got a list.
Was This Post Helpful? 0
  • +
  • -

#24 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7892
  • View blog
  • Posts: 13,421
  • Joined: 19-March 11

Re: display month and days using for loops

Posted 29 June 2013 - 09:22 AM

You want to iterate the two lists in parallel. There's a couple of ways to do this, but the for-each approach that python favors doesn't lend itself to this.

One way would be to use an index. For i in the range 0..11, you want to work with month[i] and days_Month[i]. For your inner loop, you want to get a range using days_Month[i].
If i == 2, then month[i] = "March" and days_Month[i] = 31


Work with this way for now. What follows is more idiomatic python, but if you don't get it immediately, you should hold off on it until you understand the method I just outlined. Then you can come back and figure this one out.

The zip() function takes two (N) lists and creates a list of pairs (n-tuples).

>> zip ([1,2,3], [4,5,6])
[(1,4), (2,5), (3, 6)]


You can then iterate the list of tuples as if you were iterating N lists in parallel.

As I say, if this doesn't immediately make sense to you, drop it and come back to it. You're much closer to understanding the more traditional approach, so go with that. On the other hand, if this gives you an "aha!" moment, then use that. zip is a very useful piece of python, I use it all the time.
Was This Post Helpful? 0
  • +
  • -

#25 dannii  Icon User is offline

  • New D.I.C Head

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

Re: display month and days using for loops

Posted 02 July 2013 - 06:43 AM

Jon Thank you for your help. I finally figured it out.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2