2016-12-30 10 views
2

私はあなたのBMIを計算する簡単なプログラムを書いています。私は、体重と身長の返された値を取ってBMIを計算するときに問題に遭遇しています(返される値がないかのように動作します)。このコードは、私がこの問題に遭遇した別のモジュールにすべての関数を分割したので、すべてのモジュールを1つの関数に入れたときに動作するように使います。Pythonで非整数型と整数型を掛ける

>  Error: 
>  Traceback (most recent call last): 
>  File "C:/OneDrive/Documents/3.py", line 43, in <module> 
>   bmi = calcBMI(weight, height) 
>  File "C:/OneDrive/Documents/3.py", line 17, in calcBMI 
>   bmi = float(weight * 703/(height * height)) 
>  TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' 

ここに私のコードです:

########## 
# Functions 
########## 
def getweight(): 
    weight = float(input('Enter your weight in LBs: ')) 
    if weight <= 0 or weight > 1000: 
     print('Weight cannot be less than 0 or greater than 700') 
     return weight 

def getheight(): 
    height = float(input('Enter your height in inches: ')) 
    if height <= 0: 
     print('Height cannot be less than 0') 
     return height 

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

def printData(name, bmi): 
    print(name) 
    print('Your BMI is %.2f' % bmi) 
    if bmi >= 18.5 and bmi <= 24.9: 
     print('Your BMI is normal') 
    elif bmi <= 18.5: 
     print('Your BMI is underweight') 
    elif bmi >= 25 and bmi <= 29.9: 
     print('Your BMI is overweight') 
    elif bmi >= 30: 
     print('**Your BMI is obese**') 

##################### 
# Beginning of program 
##################### 
print("Welcome to the Body Mass Index Calculator") 

name = input('Enter your name or 0 to quit: ') 

# beginning of loop 
while name != "0": 
    height = getheight() 
    weight = getweight() 
    bmi = calcBMI(weight, height) 
    printData(name, weight, height, bmi) 
    name = input('Enter another name or 0 to quit: ') 

print("Exiting program...") 

答えて

2

getheightgetweightどちらも(ifが失敗したとき、すなわち)は必ずしも数を返します。そのような場合は、Noneを返します。

+0

わかりました。今、 'if'が失敗していない場合、なぜそれが値を返さないのでしょうか?私が掘り下げて理解しているところから、私の非タイプは呼び出されたときにそれに割り当てられた値を持たないかもしれないので、タイプエラーが発生しています – No1ukno

+0

そのようなケースがどのように起こっているのですか? –

+0

私はそれが事実かどうか分からないと思います。私はそれがタイプエラーとして返されたので、私の入力値を割り当てていないと仮定していた。私はまだ学んでいるので、私はこのすべての情報を浸しています。ご返信ありがとうございます。 – No1ukno

6

最初に、0より小さい場合は高さのみを返します。ifブロック内からreturnステートメントを削除します。

例外を発生させるか、ユーザーをプロンプトに戻すなど、不適切な高さに対処するロジックを作成することもできます。間違った重みを扱うの

def getweight(): 
    weight = float(input('Enter your weight in LBs: ')) 
    if weight <= 0 or weight > 1000: 
     print('Weight cannot be less than 0 or greater than 700') 
     #Some code here to deal with a weight less than 0 
    return weight 

def getheight(): 
    height = float(input('Enter your height in inches: ')) 
    if height <= 0: 
     print('Height cannot be less than 0') 
     #Some code here to deal with a height less than 0 
    return height 

一つの方法は、次のようになります。

def getweight(): 
    while True: 
     weight = float(input('Enter your weight in LBs: ')) 
     if weight <= 0 or weight > 1000: 
      print('Weight cannot be less than 0 or greater than 700') 
     else: 
      return weight 

あなたは反復の一定数にこれを制限することができます - あなた次第。

+0

さらに良い:(それぞれに)ループを追加する。 –

+0

ありがとうございました!私は間違いがどれほどシンプルか分かりませんでした。私は両方のモジュールからifブロックを削除し、コードは必要に応じて動作します。 – No1ukno

関連する問題