本文目錄一覽:
- 1、Python小白一枚,自己寫的BMI指數計算器,求教高手一下代碼如何重複輸入以及如何結束循環?
- 2、用C#編寫一個計算體重指數的控制台程序。
- 3、怎樣用python計算bmi
- 4、求一道Python題,是關於定義函數和身體指數的,謝謝各位大神啦!!!
- 5、python簡單題不會,求解答
Python小白一枚,自己寫的BMI指數計算器,求教高手一下代碼如何重複輸入以及如何結束循環?
想讓程序循環,在最外層套一個while就可以了,想跳出的時候寫break就可以了。
在你的代碼中,while不該套在if外面,其次像你這樣判斷直接用if,就可以了,不需要elseif,直接if效率更高。
想跳出,只要寫條件執行break就行,比如下面我的代碼中,輸入N就結束,輸入Y就繼續。
while(1):
print(‘Welcome to use calculator of BMI exponent for human:’)
w = float(input(‘Please enter your weight(kg):’))
h = float(input(‘Please enter your height(m):’))
BMI = w / (h * h)
if BMI 18.5: print(‘you are thin !’)
if 18.5 = BMI =24.9: print(‘you are normal !’)
if BMI =25: print(‘you are little fat !’)
if 25.0 BMI =29.9: print(‘you are more little fat !’)
if 30.0 = BMI = 34.9: print(‘you are fat !’)
if 35.0 = BMI =39.9: print(‘you are serious fat !’)
if BMI =40: print(‘you are extreme fat !’)
print(‘continue?Y/N’)
n=”
while(n!=’Y’ and n!=’N’):
n=input()
if(n==’N’):
break
用C#編寫一個計算體重指數的控制台程序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“體重指數計算器 \n 使用幫助:根據提示輸入,再按\”回車確定\” “);
start:
Console.WriteLine(“輸入你的身高:單位:米(m)”);
double height = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(“輸入體重: 單位:千克(kg)”);
double weight = Convert.ToDouble(Console.ReadLine());
if (height = 0 || weight = 0)
{
Console.WriteLine(“你輸入的數字有誤,按\’1\’重新開始! 注意:按其他鍵將會退出程序!”);
byte i = Convert.ToByte(Console.ReadLine());
switch (i)
{
case 1:
goto start;
break;
default:
return;
break;
}
}
else
{
double bmi = weight / (height * height);
Console.WriteLine(“你的體重指數為{0}”,bmi );
if (bmi 18.5)
Console.WriteLine(“偏瘦,危險性:低,但其它疾病危險性增加”);
else if (bmi = 18.5 bmi 24)
Console.WriteLine(“正常,請繼續保持”);
else if (bmi = 24 bmi 27)
Console.WriteLine(“偏重,危險性:小”);
else if (bmi = 27 bmi 30)
Console.WriteLine(“肥胖,危險性:中”);
else if (bmi = 30 bmi 40)
Console.WriteLine(“重度肥胖,危險性:大”);
else
Console.WriteLine(“極度肥胖,危險性:極大”);
}
Console.WriteLine(“\n是否想重新使用? 是按\’1\’ 否按\’2\’來退出程序。”);
byte a = Convert.ToByte(Console.ReadLine());
switch (a)
{
case 1:
goto start;
break;
case 2:
return;
break;
default:
return;
break;
}
Console.ReadKey();
}
}
}
怎樣用python計算bmi
weight=int(raw_input(“請輸入體重(千克):”))
height=int(raw_input(“請輸入身高(米):”))
BMI=weight/(height*height)
print “BMI=”,BMI
if BMI19:
print「輕體重”
elif BMI=19 and BMI25:
print”健康身體”
elif BMI=25 and BMI28:
print”超重「
else:
print”肥胖」
raw_input(“press any key to quit.”)
求一道Python題,是關於定義函數和身體指數的,謝謝各位大神啦!!!
按照題目要求編寫的Python程序如下
def calBMI(height,weight):
BMI=weight/(height*height)
if BMI18.5:
return [BMI,”過輕”]
elif BMI24:
return [BMI,”正常”]
elif BMI28:
return [BMI,”過重”]
else:
return [BMI,”肥胖”]
import re
s=input(“請輸入你的身高(米)和體重(公斤)【逗號隔開】:”)
s1=re.split(r'[,,]’,s)
height=float(s1[0])
weight=float(s1[1])
name=”李子健”
bmi=calBMI(height,weight)
print(“{}的測算結果為:”.format(name))
print(“BMI:%.2f”%bmi[0])
print(bmi[1])
源代碼(注意源代碼的縮進)
python簡單題不會,求解答
#第一題:
from __future__ import division
print ‘請依次輸入體重(kg)與身高(m):’
weight = float(raw_input())
height = float(raw_input())
print “{:.2f}”.format(weight/(height**2))
#第二題:
print ‘請輸入一個秒數:’
sec = int(raw_input())
print str(sec/3600)+’ ‘+str(sec%3600/60)+’ ‘+str(sec%60)
#第三題:
from __future__ import division
import math
print ‘請依次輸入三角形三邊值a, b ,c:’
a = int(raw_input())
b = int(raw_input())
c = int(raw_input())
print “{:.1f}”.format(math.degrees(math.acos((a**2 + b**2 – c**2)/(2*a*b))))
你複製的 問題還複製不全,汗啊。。。這麼多問題 連個分也沒有。。。人家計算BMI是用的平方,你這裡還給了個錯的公式,還能不能認真點兒。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/195959.html