1
我已經搜索了互聯網,並嘗試了代碼的變體,但我只是不明白爲什麼我會得到輸出「無」結果,當時我在Udacity的計算機科學入門2課中學習PS 2。Udacity進入計算機科學(Python)第2課PS郵票 - 輸出包括「無」
這裏是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
誰能請解釋一下其中的「無」是哪裏來的?
如果不定義'return'聲明'None'將採取系統默認返回。您需要添加'return smth'或不打印'stamps'的結果。 –
Incroyable ...繼續搜索另外5分鐘,自己找到答案:我打印結果而不是返回它們。電話打印返回值... Thanx爲您的幫助! – Bella