2017-02-22 28 views
0

我在我所需的大学编程课程的第3周。我被赋予计算BMI的任务。我可以编写产生所需输出的代码,但指令说我必须使用函数,这给我带来了麻烦。这里是我提出的代码,我相信也许我的问题是,在第12,16和20行,输入值没有存储在函数名称中。另外别的东西似乎是错的,因为当我运行它时,它要求两次学生身高。如果有人可以看看这个,给我一些方向,我会非常感激。函数名不存储python中的返回值

说明...

你是当地高中足球队的营养教练。你意识到一些球员在夏季休息时没有达到标准。实现营养是高效团队的关键,您决定实施身体质量指数计划。 撰写模块化的身体质量指数(BMI)程序,该程序将计算团队成员的BMI。计算体重指数的公式如下: 体重指数=体重* 703 /身高^ 2 注意:身高^ 2意思是身高增加到2的幂。 您的程序应该使用以下功能: A方法,以获得一个玩家 以获得玩家 来计算玩家 显示身高,体重的方法的BMI的方法的高度的方法的重量,并计算BMI

import math 
import sys 
print("King's BMI Calculator") 

while True: 
    name = input("Please enter student's name or press 0 to quit:") 
    if name == "0": 
     break 

    def h(): 
     height = int(input("Please enter student's height in inches:")) 
     return height 

    def w(): 
     weight = int(input("Please enter student's weight in pounds:")) 
     return weight 

    def bmi(): 
     total = float((str(w() * 703)/str(h() * str(h())))) 
     return total 

    def printbmi(): 
     print(name + "'s BMI Profile") 
     print("Height:", str(h(), "inches")) 
     print("Weight:", str(w(), "lbs")) 
     print("BMI Index:" + str(float(round(bmi(), 1)))) 
     return 

    def main(): 
     h() 
     w() 
     printbmi() 

    main() 
+2

什么是“输入值不被存储在函数名称”是什么意思?自从你打电话给'h'两次后它会要求两次高度。 – Carcigenicate

+0

http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html#variable-scope-and-lifetime – ryannjohnson

+0

这不是教程服务。你需要问一个*特定的问题*。但是这里有一些你应该考虑的问题 - 为什么你要在while循环中定义你的函数?在外面定义它们并在里面调用它们。此外,你会被提示输入两次,因为*这正是你编码的*。你可以在'main'中调用'h'和'w',并在'printbmi'中再次调用'main'。你从来没有保存过任何返回的值,所以它看起来毫无意义。只需在'main'中删除对'h'和'w'的呼叫,你就可以顺利地进行... –

回答

0

每当你调用一个函数(即function_name()),该函数被执行。因此,无论何时调用w()h(),脚本都会要求输入。一个简单的解决方案是将返回值存储到一个变量中,可能为weight = w()height = h(),以便您可以在需要时使用变量,而不是每次调用整个函数。

def w(): 
    # ... 
    return weight 

def h(): 
    # ... 
    return height 

def bmi(w, h) 
    # ... 
    return total 

def printbmi(w, h, total): 
    # print(w, h, total) 

def main(): 
    # prompt 
    weight = w() 
    height = h() 
    total = bmi(weight, height) 
    printbmi(weight, height, total) 

while True: 
    main() 
0

您可以考虑以下事项来更新您的代码。

  • 定义while循环外的函数(h(),w(),bmi())。
  • 请勿在main()之内拨打h()w(),因为您在拨打printbmi()。所以,你的程序会为每个学生要求输入两次。
  • 您应该将main()函数也移到循环之外。

您的printbmi()和bmi()函数也有问题。您多次拨打h()w()函数。

您可以更新您的代码,如下所示。

import math 
import sys 

def h(): 
    height = int(input("Please enter student's height in inches: ")) 
    return height 

def w(): 
    weight = int(input("Please enter student's weight in pounds: ")) 
    return weight 

def bmi(height, weight): 
    total = float((weight * 703)/(height * height)) 
    return total 

def printbmi(name): 
    print(name + "'s BMI Profile") 
    height = h() 
    print("Height:", str(height), "inches") 
    weight = w() 
    print("Weight:", str(weight), "lbs") 
    print("BMI Index:" + str(float(round(bmi(height, weight), 1)))) 

def main(name): 
    printbmi(name) 


print("King's BMI Calculator") 
while True: 
    name = input("Please enter student's name or press 0 to quit:") 
    if name == "0": 
     break 
    else: 
     main(name) 

它输出:

King's BMI Calculator 
Please enter student's name or press 0 to quit:Jack 
Jack's BMI Profile 
Please enter student's height in inches: 12 
Height: 12 inches 
Please enter student's weight in pounds: 16 
Weight: 16 lbs 
BMI Index:78.1 
Please enter student's name or press 0 to quit:0