[英]How do I stop my BMI calculator from adding an unwanted “None” when giving the BMI value?
我創建了一個BMI計算器,作為一個簡單的初學者項目。 一切正常,除了代碼在額外的一行顯示“無”的情況下輸出了多余的多余文本。
這是代碼:
def bmi_calculator(height, weight):
bmi = weight / (height ** 2)
print("Your bmi is:")
print(bmi)
if bmi > 25:
print("You are overweight")
else:
print("You are healthy")
a = input("What is you're height in metres?")
height = int(a)
b = input("What is you're weight in kilograms?")
weight = int(b)
print(bmi_calculator(height, weight))
我希望輸出是(例如):
Your bmi is:
60.0
You are overweight
但是實際輸出是:
Your bmi is:
60.0
You are overweight
None
主程序中的print()
應該是一個簡單的函數調用。 該None
是print
顯示你的函數的返回值-但你的函數沒有返回值; 默認為None
。 該功能執行所需的所有打印。
a = input("What is you're height in metres?")
height = int(a)
b = input("What is you're weight in kilograms?")
weight = int(b)
bmi_calculator(height, weight)
發生這種情況是因為您正在打印函數bmi_calculator()
返回的內容。 在python中,如果不存在顯式返回,則所有函數均返回None。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.