3 Replies - 617 Views - Last Post: 29 December 2013 - 10:37 PM Rate Topic: -----

#1 zoltman1991   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 27-December 13

Why does the 'print' fxn do this in this code?

Posted 29 December 2013 - 09:11 PM

def printgrades( grades ):
    students = len( grades )    # number of students
    exams = len( grades[ 0 ] )  # number of exams          # why the [ 0 ] in this one?
    
    # print table headers
    print "The list is:"
    print "            "
    
    for i in range( exams ):
        print "[ %d ]" % i,
        
    print  # THIS PRINT HERE
    
    # print scores, by row
    for i in range( students ):
        print "Grades[ %d ]   " % i,
        
        for j in range( examss ):
            print grades [ i ] [ j ], "",
            
        print 


Look at the print with the comment.
When I remove this, it prints out the entire table in a linear form with one row. When print is present, it prints it correctly - as a table with rows and columns.

I don't understand why it does this? What the heck is it doing that's so important. I would have thought that it would just print a blank line.

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: Why does the 'print' fxn do this in this code?

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1647
  • View blog
  • Posts: 8,510
  • Joined: 07-September 06

Re: Why does the 'print' fxn do this in this code?

Posted 29 December 2013 - 10:20 PM

The docs say this:

Quote

A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.


So, basically, a print statement without any string or arguments just prints out a newline. A print statement without a comma at the end will print a newline (if it has a comma at the end it won't).
Was This Post Helpful? 1
  • +
  • -

#3 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Why does the 'print' fxn do this in this code?

Posted 29 December 2013 - 10:22 PM

It's your comma following the previous print statement. Commas in methods separate the arguments. With that comma you are indicating you have another argument to the function, but you don't have one, only a print statement and so it uses the print. This would create lines after each iteration of the for loop. That is what I believe it to be. I'm not completely sure.
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11743
  • View blog
  • Posts: 19,938
  • Joined: 19-March 11

Re: Why does the 'print' fxn do this in this code?

Posted 29 December 2013 - 10:37 PM

BetaWar and salazar have nailed it.

As for this question:
exams = len( grades[ 0 ] )  # number of exams          # why the [ 0 ] in this one?


This gets you the length of one row. The assumption is that all rows have the same length. If this assumption turns out to be incorrect, all manner of terrible things might happen. Plague, famine, pestilence, array out of bounds exceptions, that sort of thing. (But mostly the latter.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1