Python 3.x 函数名未在python中存储返回值

Python 3.x 函数名未在python中存储返回值,python-3.x,Python 3.x,我正在上大学编程必修课的第三周。我被指派计算体重指数。我可以编写生成所需输出的代码,但指令说我必须使用函数,这给我带来了麻烦。这是我提出的代码,我相信我的问题可能是在第12、16和20行,输入值没有存储在函数名中。还有一点似乎是错误的,因为当我运行它时,它要求学生身高两倍。如果有人能看看这个,给我一些指导,我会非常感激 说明 你是当地高中足球队的营养教练。你知道有些球员在暑假结束后没有达到标准。意识到营养在富有成效的团队中起着关键作用,您决定实施体重指数计划。 编写一个模块化体重指数(BMI)程

我正在上大学编程必修课的第三周。我被指派计算体重指数。我可以编写生成所需输出的代码,但指令说我必须使用函数,这给我带来了麻烦。这是我提出的代码,我相信我的问题可能是在第12、16和20行,输入值没有存储在函数名中。还有一点似乎是错误的,因为当我运行它时,它要求学生身高两倍。如果有人能看看这个,给我一些指导,我会非常感激

说明

你是当地高中足球队的营养教练。你知道有些球员在暑假结束后没有达到标准。意识到营养在富有成效的团队中起着关键作用,您决定实施体重指数计划。 编写一个模块化体重指数(BMI)程序,计算团队成员的BMI。计算体重指数的公式如下: 体重指数=体重*703/身高^2 注:高度^2表示将高度值提升到2的幂。 您的程序应使用以下功能: 一种获得运动员体重的方法 一种获得运动员身高的方法 一种计算运动员体重指数的方法 显示身高、体重和计算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()

无论何时调用函数(即
function\u 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()

您可以通过考虑以下内容来更新代码

  • 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

“输入值未存储在函数名中”是什么意思?因为你打了两次电话给
h
,所以它会两次询问高度。这不是辅导服务。你需要问一个具体的问题。但是这里有一个你应该考虑的问题——为什么你要在while循环中定义你的函数?在外部定义它们并在内部调用它们。此外,系统会提示您输入两次,因为这正是您编写的代码。在
main
中调用
h
w
,然后在
printbmi
中再次调用
main
中调用的
printbmi
。您在任何时候都不会保存任何返回的值,因此这似乎毫无意义。只需删除
main
中对
h
w
的调用,您就可以顺利上路了……而且,您正在
bmi
中调用
h
w
!实际上,您在
bmi
中调用了
h
两次。每次调用函数时,函数中的代码都会运行。保存对一个变量的一次调用的结果,然后将该变量传递到其他函数中……我明白你对两次调用它的意思了。我会努力的,谢谢