工程與科技
大學
已解決

第3 16 17條完全不知道要打什麼
用while true算BMI能算完一組數據再直接算下一組不用重跑嗎?
有既定的數據要用if設range嗎?
Python初學真的問題很多抱歉(;´༎ຶД༎ຶ`)

3 4 600 v0u - W NE while True: try: height, weight = input("height, weight:").split("") height = float("height") weight = float("weight") bmi = weight/((height/100)**2) if bmi <= 18.5 : print("underweight") elif 18.5 < bmi <= 25 : print("normal weight") elif 25 < bmi <= 30 : print("overweight") elif 30 < bmi : print("obese") 10 11 12 13 14 15 16 except: exit(print()) 17 18 19 20 I

解答

✨ 最佳解答 ✨

如果不設定while True的話輸入1筆測資後程式就結束了。while True是一個條件恆為真的迴圈,所以會持續執行。

try跟except是處理例外(exception)的狀況,在程式接收測資的時候會去try,能夠正常執行則執行,如果發生錯誤則執行except這個block底下的部分。

chou014

轉型的部分打錯了,float(height)才對,用“”包起來會變成把"height"這個字串轉成浮點數(float)。

留言
PromotionBanner
您的問題解決了嗎?