Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Udacity to Computer Science(Python)第2课PS邮票-输出包括;无”;_Python - Fatal编程技术网

Udacity to Computer Science(Python)第2课PS邮票-输出包括;无”;

Udacity to Computer Science(Python)第2课PS邮票-输出包括;无”;,python,Python,我在互联网上搜索过,并尝试过代码的变体,但我不明白为什么我在Udacity的《计算机科学导论》第2课的PS2上工作时,会在结果之间得到“无”的输出 以下是PS和我的当前状态: # 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 ma

我在互联网上搜索过,并尝试过代码的变体,但我不明白为什么我在Udacity的《计算机科学导论》第2课的PS2上工作时,会在结果之间得到“无”的输出

以下是PS和我的当前状态:

# 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
它产生输出:

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

有人能解释一下“无”的来源吗?

您正在调用打印结果的函数,然后打印函数的返回值,即
None

您应该选择一种显示数据的方法。 仅在函数内部打印:

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)
或使用
返回

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)

如果未定义
return
语句,则默认情况下将返回
None
。您需要添加
返回smth
或不打印
戳记的结果。
。不可打印。。。又搜索了5分钟,我自己找到了答案:我打印了结果,而不是返回结果。调用正在打印返回的值。。。谢谢你的帮助!