Printing values of variables to the console


#1

Hello everyone,

Sorry for the newbie question, but how do you go about printing values of variables to the console for debugging purposes? I'm currently doing the "median" lesson and I really could use some feedback...

I use the "print" instruction followed by the name of the variable (I tried it with and without parentheses), but nothing gets printed to the console...

EDIT: My code now works, but my question remains...

def median(listofnumbers):
    medianvalue=0
    listofnumbers = sorted(listofnumbers)
    print (listofnumbers)
    if len(listofnumbers)%2 == 0:
        lowmedian=listofnumbers[(len(listofnumbers)/2)-1]
        highmedian=listofnumbers[len(listofnumbers)/2]
        medianvalue = (lowmedian+highmedian)/2.0
    elif len(listofnumbers) == 1:
        medianvalue = listofnumbers[0]
    else:
        medianvalue = listofnumbers[len(listofnumbers)/2]
    return medianvalue

Thank you very much!

Etienne


#2

Then you did not execute that print statement. Consider this:

if False:
    print 'this will not print'

So how do you get the statements in a function to run?


#3

I don't understand. The "print" statement isn't in a "if"...


#4

Why not? What marks the end of the if-statement?


#5

Line 4, "print (listofnumbers)"... That's not inside a "if"...