1 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
def to_smash(total_candies):
print("Splitting", total_candies, (lambda total_candies: "candies" if total_candies > 1 else "candy")(total_candies))
return total_candies % 3
to_smash(1)
to_smash(15)
但是,請(qǐng)注意,在傳遞函數(shù)方面,Python 不如 Javascript 通用——lambda它有其局限性,特別是它只是一個(gè)單行函數(shù)。相反,我建議只在 print 語(yǔ)句之外一起定義您的函數(shù)。
def to_smash(total_candies):
def plural_or_singular(total_candies):
if total_candies>1:
return "candies"
else:
return "candy"
print("Splitting", total_candies, plural_or_singular(total_candies))
return total_candies % 3
to_smash(1)
to_smash(15)
添加回答
舉報(bào)