[英]Python - print function output
def cent_to_fahr(cent):
print (cent / 5.0 * 9 + 32)
print (cent_to_fahr(20))
輸出是這樣的:
68.0
None
為什么此輸出None
?
當我只使用cent_to_fahr(20)
時,我沒有得到這個結果。 請問為什么會這樣?
def cent_to_fahr(cent):
return (cent / 5.0 * 9 + 32)
print (cent_to_fahr(20))
一個函數需要return
一個值以具有除None
的輸出
為了將其放在上下文中,讓我們嘗試了解是什么原因導致您收到的每一行輸出:
68.0
。 這可以看作是該計算“壽命”的“終點”,該值/結果不再可用於進一步的計算。 None
是,在這種情況下也完全沒有用。 現在我們更好地了解了,我建議調整函數以返回計算出的值。
def cent_to_fahr(cent):
return (cent / 5.0 * 9 + 32)
這樣,當調用該函數時(在其他函數的上下文中),它將返回一個可以進一步處理的值(在這種情況下,使用print()
):
>>>print(cent_to_fahr(20))
它將打印68.0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.