Practice Makes Perfect


#1

My code is supposed to add the digits of itself together but when input 434, the output comes out to:
11
0
11
Can someone explain this please?

def digit_sum(n):
num = 0
n = str(n)
for i in n:
i = int(i)
num += i
print num
digit_sum(434)


#2

First, you will need to explain to us your code and what it is expected to do at each stage.


#3

it is just supposed to print 11


#4

Well, that is moot. Just how is it supposed to get 11 from 434?


#5

def digit_sum(n):
–num = 0
–n = str(n)
–for i in n:
----i = int(i)
----num += i
–print num
digit_sum(434)

It takes one character of the string and turns it into an integer then adds it to number and does it with the rest of the numbers.


#6

That should be,

return num

#7

Can you explain the difference between print and return. Don’t they do the same thing?


#8

I had anticipated this question, and it’s good that you asked.

The print statement has no return, which means there is nothing in the statement or its output that is re-usable. If we print the outcome of an expression or print literal data it is not stored, only output to the display and then vanishes.

return is the statement that gives something back to the caller (the expression that calls the function). Program flow resumes from that point with the return value in existence, assuming it is cached in a variable.

Let’s look at a couple examples.

def foo():
    print (6 * 7)

def bar():
    return 6 * 7

my_foo = foo()
my_bar = bar()

What will my_foo be? What will my_bar be?

>>> def foo():
    print (6 * 7)

    
>>> def bar():
    return 6 * 7

>>> my_foo = foo()
42
>>> my_bar = bar()
>>> print (my_foo)
None
>>> print (my_bar)
42
>>> 

#9

But I have noticed that when I use return True or return False, it does not print True or False so does return print what is in it?


#10

return is not related to print in any way. Think of print as a function (which it is in Python 3). My above example exhibits the behavior of a function.

Notice how foo() does not have a return statement, only a print statement? In others words, what was the return? There was none (pun intended).

>>> def foo():
    print (6 * 7)
>>> my_foo = foo()
42

The above number was printed by the function. But that is not the value cached in my_foo.

>>> print (my_foo)
None

my_foo cached the return value, of which there was none. So when we printed it the interpreter supplied a defacto token as a placeholder.

It does go one step deeper. Since print is a function, we can anticipate a return, else None. However, that has no bearing unless we return the print function.

>>> def none_check():
    return print ("something")

>>> none_check()
something
>>> a = none_check()
something
>>> a
>>> 

Notice the last line and how nothing printed? The console has no printable character for None. In the console, every entry is a command that gets a response. The response to None is nothing.

The print function can suss out a non-value, and is useful in debugging to catch these nasty little devils when they elude us otherwise.

>>> print (a)
None
>>> 

The bottom line is that we should in no way equate print to return. They have no relation whatsoever. Follow the unit path and see many more examples of their differences. Once you catch on, you will no longer see them as similar. They are not.


#11

When we wish to deliver a message, we send in a messenger. When we wish to obtain information we send in an emissary. The messenger’s information is printed, the emissary’s is returned.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.