我在互联网上搜索过,尝试过不同的代码,但我不明白为什么我在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}$谁能解释一下“无”是从哪里来的吗?在
调用打印结果的函数,然后打印函数的返回值
None
。在您应该选择一种显示数据的方法。 只在函数内部打印:
或使用
^{pr2}$return
:相关问题 更多 >
编程相关推荐