计算机科学(Python)第2课PS图章输出包括“无”

2024-06-17 19:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

以下是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

产生输出:

^{pr2}$

谁能解释一下“无”是从哪里来的吗?在


Tags: andofthenonumberstampasone
1条回答
网友
1楼 · 发布于 2024-06-17 19:15:38

调用打印结果的函数,然后打印函数的返回值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)

或使用return

^{pr2}$

相关问题 更多 >