[英]Defining functions in Python
我有這樣的Python代碼,
def cube(n):
n = n * n * n
return n
def by_three(n):
if (n % 3 == 0):
print "number is divisible by three"
cube(n)
return n
else:
print "number is not divisible by three"
return False
目前,我無法返回任何值,請讓我知道問題出在哪里?
您沒有在要返回的by_three
函數中設置cube(n)
的值。
def cube(n):
result = n ** 3 # You can use ** for powers.
return result
def by_three(n):
if (n % 3 == 0):
print "number is divisible by three"
result = cube(n)
return result
else:
print "number is not divisible by three"
return False
我還假設您在復制到SO時縮進了一個錯誤。 如果不是這種情況,那是您的原始縮進,那么您也需要糾正這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.