1

I have searched the internet and tried variations of the code, but I just don't see why I get the Output "None" between the results, when I work on the PS 2 in Lesson 2 of Udacity's Intro to Computer Science.

Here is the PS and my current status:

# Define a procedure, stamps, which takes as its input a positive integer in
# pence and returns the number of 5p, 2p and 1p stamps (p is pence) required 
# to make up that value. The return value should be a tuple of three numbers 
# (that is, your return statement should be followed by the number of 5p,
# the number of 2p, and the nuber of 1p stamps).
#
# Your answer should use as few total stamps as possible by first using as 
# many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as 
# needed to make up the total.
#


def stamps(n):
    if n > 0:
        five = n / 5
        two = n % 5 / 2
        one = n % 5 % 2
        print (five, two, one)
    else:
        print (0, 0, 0)


print stamps(8)
#>>> (1, 1, 1)  # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0)  # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0)  # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps

which produces the output:

(1, 1, 1)
None
(1, 0, 0)
None
(5, 2, 0)
None
(0, 0, 0)
None

Can anyone please explain where the the "None" is coming from?

Bella
  • 63
  • 6
  • If you don't define `return` statement `None` will be returned be default. You need to add `return smth` or not print the result of `stamps`. – Gennady Kandaurov Nov 27 '16 at 08:46
  • Incroyable... Kept searching for another 5 minutes and found the answer myself: I printed the results instead of returning them. The call is printing the returned values... Thanx for your help! – Bella Nov 27 '16 at 08:47

1 Answers1

1

You are calling the function that prints the result and then you are printing the function's return value which is None.

You should pick one method of displaying the data. Either print only inside the function:

def stamps(n):
    if n > 0:
        five = n / 5
        two = n % 5 / 2
        one = n % 5 % 2
        print five, two, one
    else:
        print 0, 0, 0

stamps(8)
stamps(5)
stamps(29)
stamps(0)

Or use return:

def stamps(n):
    if n > 0:
        five = n / 5
        two = n % 5 / 2
        one = n % 5 % 2
        return five, two, one
    else:
        return 0, 0, 0


print stamps(8)
print stamps(5)
print stamps(29)
print stamps(0)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154