私はあなたの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...")
わかりました。今、 'if'が失敗していない場合、なぜそれが値を返さないのでしょうか?私が掘り下げて理解しているところから、私の非タイプは呼び出されたときにそれに割り当てられた値を持たないかもしれないので、タイプエラーが発生しています – No1ukno
そのようなケースがどのように起こっているのですか? –
私はそれが事実かどうか分からないと思います。私はそれがタイプエラーとして返されたので、私の入力値を割り当てていないと仮定していた。私はまだ学んでいるので、私はこのすべての情報を浸しています。ご返信ありがとうございます。 – No1ukno