我正在为BMI计算器编写快速代码,为什么会发生错误“缺少预期返回“字符串”的函数中的返回”?谁能解释这个原因?谢谢!
func bmiCalculator (weight: Double, height: Double) -> String {
let bmi = weight / (height * height)
if bmi > 25 {
return "Your BMI is \(bmi). You are overweight"
} else if bmi >= 18.5 && bmi <= 25 {
return "Your BMI is \(bmi). You are at a normal weight"
} else if bmi < 18.5 {
return "Your BMI is \(bmi). You are underweight"
}
}
print(bmiCalculator(weight: 70, height: 1.74))
我希望Xcode Playground将根据最后一行的体重和身高值来计算BMI,但是它显示错误“在期望返回“字符串”的函数中缺少返回值”
答案 0 :(得分:1)
尽管您似乎已经完全覆盖了此处的值,所以实际上不可能实现其他任何值,但在未完全分析上下文的情况下检查了语法。它希望仍然能够确定其返回的所有结果都以String
结尾。
func bmiCalculator (weight: Double, height: Double) -> String {
let bmi = weight / (height * height)
if bmi > 25 {
return "Your BMI is \(bmi). You are overweight"
} else if bmi < 18.5 {
return "Your BMI is \(bmi). You are underweight"
}
return "Your BMI is \(bmi). You are at a normal weight"
}
答案 1 :(得分:0)
您的方法必须返回df2[df1['Effective_Date'].astype(int) > df2] = 0
的值,但是在没有条件为真的情况下,您缺少返回值:String
有三种可能的情况:
bmi> 25
bmi> = 18.5 && bmi <= 25
bmi <18.5
...所以好的做法是使用else {...}
语句
switch