我正在使用此BMI计算器代码,但是当我运行程序并输入所有值时,它给出了答案,但仍在重复。我希望程序在给出答案后终止。 我也希望答案以小数点表示。
下面是代码:-
import math
print "*After Entering Every value Hit Enter"
time.sleep(2)
#intro Starts
name = raw_input("Please Enter Your Name : ") #enter your name
print""
print "Hello", name
time.sleep(0.5)
print""
print "It Feels Like Your Are Really Concerned About Your Health"
time.sleep(3)
print "So Lets Start Calculating Your BMI"
print "" #intro ends
#option for gender
gender = raw_input('Are you Male Or female ? ')
print "OK, Thats great"
print ""
#the bmi Calculation
kg = float(input('Enter Your Weight(in Kg.) : '))
cm = float(input('Enter Your height(in cm) : '))
print ""
meter = cm * math.pow(10, -2)
bmi = float(kg//(meter**2))
#the Conditions start
while gender == "male":
if bmi < 18.5: #if overweight
print "It seems like your are Underweight"
print "Your BMI is", bmi
print ("")
print "I prefer you To Increase Your diet."
#if normal
elif bmi >= 18.5 and bmi <= 25:
print "Your are Absolutely fine"
print "Your BMI is", bmi
print "Be on the same track"
#if overweight
elif bmi > 25 and bmi <=30:
print "Your are overweight "
print "Your BMI is", bmi
print "I prefer you to go for daily morning Jog."
#if obese
elif bmi > 30:
print "Your are under obese category"
print "Your BMI is", bmi
print "I prefer you to Change your diet"
while gender == "female" or "Female" :
print "sorry this program is under development"
print ("")
如果有人帮助我,我将很感激。 我是初学者
答案 0 :(得分:1)
因此,如果您不想重复此过程,则应尝试考虑使用if
而不是while
import math
import time
print "*After Entering Every value Hit Enter"
time.sleep(2)
#intro Starts
name = raw_input("Please Enter Your Name : ") #enter your name
print""
print "Hello", name
time.sleep(0.5)
print""
print "It Feels Like Your Are Really Concerned About Your Health"
time.sleep(3)
print "So Lets Start Calculating Your BMI"
print "" #intro ends
#option for gender
gender = raw_input('Are you Male Or female ? ')
print "OK, Thats great"
print ""
#the bmi Calculation
kg = float(input('Enter Your Weight(in Kg.) : '))
cm = float(input('Enter Your height(in cm) : '))
print ""
meter = cm * math.pow(10, -2)
bmi = float(kg//(meter**2))
#the Conditions start
if gender == "male":
if bmi < 18.5: #if overweight
print "It seems like your are Underweight"
print "Your BMI is", bmi
print ("")
print "I prefer you To Increase Your diet."
elif bmi >= 18.5 and bmi <= 25:
print "Your are Absolutely fine"
print "Your BMI is", bmi
print "Be on the same track"
#if overweight
elif bmi > 25 and bmi <=30:
print "Your are overweight "
print "Your BMI is", bmi
print "I prefer you to go for daily morning Jog."
#if obese
elif bmi > 30:
print "Your are under obese category"
print "Your BMI is", bmi
print "I prefer you to Change your diet"
if gender in ("female" ,"Female" ): #gender == "female" or "Female" is always true
print "sorry this program is under development"
print ("")
答案 1 :(得分:0)
使用break命令中断循环:
while gender == "male":
if bmi < 18.5: #if overweight
print "It seems like your are Underweight"
print "Your BMI is", bmi
print ("")
print "I prefer you To Increase Your diet."
#if normal
elif bmi >= 18.5 and bmi <= 25:
print "Your are Absolutely fine"
print "Your BMI is", bmi
break
此外,别忘了在循环结束时添加等待,这样程序就不会在完成后立即退出:
import time
time.sleep(100)
您编辑了代码,所以我也编辑和改进了您的代码:
while gender == "male":
if bmi < 18.5: #if overweight
print "It seems like your are Underweight"
print "Your BMI is", bmi
print ("")
print "I prefer you To Increase Your diet."
#if normal
elif bmi <= 25:
print "Your are Absolutely fine"
print "Your BMI is", bmi
print "Be on the same track"
#if overweight
elif bmi <=30:
print "Your are overweight "
print "Your BMI is", bmi
print "I prefer you to go for daily morning Jog."
#if obese
elif bmi > 30:
print "Your are under obese category"
print "Your BMI is", bmi
print "I prefer you to Change your diet"
break
while gender.lower() == "female":
print "sorry this program is under development"
print ("")
我刚刚尝试运行您的程序。您的代码有很多错误和错误。您不是在导入time
而是在等待它。 \n
表示换行符。无需编写print ""
,而只需在旧的\n
语句中您想在新行插入print
的地方。如果我们用性别写Male
,那么条件将不会运行。请尝试以下操作:
import math,time
print "*After Entering Every value Hit Enter"
time.sleep(2)
#intro Starts
name = raw_input("Please Enter Your Name : ") #enter your name
print "\nHello\n", name
time.sleep(0.5)
print "It Feels Like Your Are Really Concerned About Your Health"
time.sleep(3)
print "So Lets Start Calculating Your BMI\n"
#option for gender
gender = raw_input('Are you Male Or female ? ')
gender = gender.lower()
if gender != "male" and gender != "female":
print "Invalid Gender"
time.sleep(10)
quit()
print "OK, Thats great\n"
#the bmi Calculation
kg = float(input('Enter Your Weight(in Kg) : '))
cm = float(input('Enter Your height(in cm) : '))
print ""
meter = cm * math.pow(10, -2)
bmi = float(kg//(meter**2))
#the Conditions start
while gender == "male":
if bmi < 18.5: #if overweight
print "It seems like your are Underweight\nYour BMI is\n", bmi,"\nI prefer you To Increase Your diet."
elif bmi < 25:
print "Your are Absolutely fine\nYour BMI is", bmi,"\nBe on the same track\n"
elif bmi <=30 :
print "Your are overweight \nYour BMI is", bmi,"\nI prefer you to go for daily morning Jog."
elif bmi > 30:
print "Your are under obese category\nYour BMI is", bmi,"\nI prefer you to Change your diet"
break
while gender == "female":
print "Sorry! this program is under development"
break
print ("")