Simpler way to print multiple lines?


#1

Is there a nicer way to do this? Typing print five times seems amateurish.

print grades
print sum(grades)
print grades_average(grades)
print grades_variance(grades)
print grades_std_deviation(variance)

`


#2

Yes, and no. Formatting output to the display is a task in itself, and at this stage I would say you are wasting your time with this. Chug through the track, get some practice and then come back to this question.

There should be a module on string formatting and console output. If you haven't taken it yet, come back to this once you complete it, otherwise perhaps go back and review.


#3

Okay so I figured out the "professional" way to do it looks like this:

print "%r\n%r\n%r\n%r\n%r" % (print_grades(grades), grades_sum(grades), grades_average(grades), grades_variance(grades), grades_std_deviation(variance))

The problem is it prints an extra "None" before the grades_sum function so it's not correct.
Any help?


#4

The None is the return value from this function (as in, there is none).

Try,

', '.join(grades)

in that parameter (argument, actually). Then you get a flat list (in string form).for this value.

Then again, you are using repr() so it will print the array inline.

grades

is all you need in that parameter. All the other inputs are single number values which your format string can zero in on for added validation:

print "%.2f" % 3.1415          # 3.14

where the .2 is the decimal places to print. This does not mutate the actual value, only the print representation. If a non-number is passed in to this parameter it will raise an error.

print "%.2f" % float('inf')    # inf
print "%.2f" % 'inf'

Traceback (most recent call last):
File "", line 4, in
TypeError: float argument required, not str