2017-02-23 29 views
0

27行目で合計が定義されていないというエラーが表示されます。以前これを修正するために、7行目と12行目にglobalを追加しましたので、17行目に追加しようとしましたが、それでもエラーが表示されます。どのように私はこれを修正することができますどのような考え?私のコードに関するその他のヒントや情報も大変ありがとうございます。私はコーディングに新しいです。私は、PythonPythonの関数内で変数を定義する方法

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


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

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

def bmi(): 
    global total 
    total=((str(weight) * 703)/(str(height) * str(height))) 
    return total 

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

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

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

    main() 
+0

に値を格納している、bmi()を呼び出すためには表示されませんので、この場合は

は、あなたは、どこでも合計を定義していませんこっちで。 27行目は何ですか? –

+0

ああ、申し訳ありません。私の行番号はとにかくすべて乱されています... –

+0

エラーは... at print( "BMI Index:" + str(float(round、total、1)))) NameError:name 'total'が定義されていません –

答えて

0

で働いているあなたは、グローバル変数を変更しているときglobalを使用する必要があります。変数が定義されている限り、変数を読み込んでいるときに使用する必要はありません。あなたのコードがwh()printbmi()を呼び出しますが、唯一の場所は、あなたが私たちを助けtotal.

+0

AH HA !!!多くのsooをありがとう!それが私の問題でした。私は決して機能を呼び出さなかった。 –

関連する問題