how to get the product after the colons - python

so im learning this program BMI calculator, and i want the answers to be written after the ':', not under it. can anyone help me pls? is it even possible to get that type of answer?
heres the code:
name = "hello"
height_m = 2
weight_kg = 110
bmi = weight_kg / (height_m ** 2)
print("bmi:")
print(bmi)
if bmi < 25:
print(name)
print("is not overweight")
else:
print(name)
print("is overweight")
#printed:
bmi:
27.5
hello
is overweight
#but what i want is this:
bmi: 27.5
hello is overweight

You need to put a comma in the same print statement instead of two separate ones.
print("bmi:",bmi)
instead of
print("bmi:")
print(bmi)

Try this
name = "hello"
height_m = 2
weight_kg = 110
bmi = weight_kg / (height_m ** 2)
print("bmi: {}".format(bmi)
if bmi < 25:
print("{} is not overweight".format(name))
else:
print("{} is overweight".format(name))
#prints:
bmi: 27.5
hello is overweight

Try this,
name = "hello"
height_m = 2
weight_kg = 110
bmi = weight_kg / (height_m ** 2)
print("bmi:", end=' ')
print(bmi)
if bmi < 25:
print(name, end=' ')
print("is not overweight")
else:
print(name, end=' ')
print("is overweight")
Or you can put everything in a single print statement like this,
print(name, "is overweight")
print("bmi:", bmi)

You can either do:
print("bmi:", bmi) # the simplest
or
print(f"bmi: {bmi}") # f string in Python 3
or
print("bmi: ", end="") # Your original code, modified
print(bmi)
or
print("bmi: %s " % bmi) # too extreme -- don't do it for this simple case
or
print("bmi: {}".format(bmi)) # too extreme

Related

While loop is not executing at all and everything else looks right

I cannot get this while loop to work. Every time, it says something is wrong with it and I have NO idea what. I've tried capitalizing and uncapitalizing, tabbing, just about everything. I'm truly at my wits end, please help
def inputM():
print("Enter weight in kg")
weightm = float(input())
print("Enter heigh in meters")
heightm = float(input())
return weightm, heightm
def inputI():
print("Enter weight in pounds")
weighti = float(input())
print("Enter height in inches")
heighti = float(input())
return weighti, heighti
def healthindex (BMIList, BMINum, bmi, healthy):
if healthy == "b":
print (str(bmi))
elif healthy == "h":
index = 0
print ("Your bmi is" + (str(bmi))
while index < len(BMIList):
if bmi < BMINum[index]:
print ("And, you are " + BMIList[index])
return
index = index + 1
print("You are Obese")
return
BMIList = ["severly underweight", "underweight", "healthy", "overweight", "obese"]
BMINum = [12, 18.4, 24.9, 29.9, 200]
print("Welcome to BMI Calculator!")
print("Enter I for Imperial or M for Metric")
request = input().upper()
if request == "M":
weightm, heightm = inputM()
bmi = weightm/(heightm**2)
elif request == "I":
weighti, heighti = inputI()
bmi = (703*weighti)/(heighti**2)
else:
print("Invalid input")
print("Enter b to only see your bmi or enter h if you would like to see your bmi and health index")
healthy= input()
healthindex (BMIList, BMINum, bmi, healthy)
You have a syntax error in the print statement above the while loop. You are missing the closing parenthesis as you can see from the following snippet of your code:
print ("Your bmi is" + str(bmi)
while index < len(BMIList):

bmi calculator how do restart the calculator to go back to yes/no at the top

Take_Bmi=(input("Take bmi yes or no "))
if Take_Bmi == "yes":
name1=input(" enter your name")
height_m1=input(" enter your height in m")
weight_kg1=input(" enter your weight")
def bmi_calculator(name1,height_m1,weight_kg1):
bmi = float(weight_kg1) / (float(height_m1)** 2)
#The input function returns a string. So to get your output you
#need to use "float()" for height and weight:
print("bmi: ")
if bmi < 25 :
print(bmi)
return name1 + " not overweight"
else:
print(bmi)
return name1 + " is overweight"
result= bmi_calculator(name1,float(height_m1),float(weight_kg1))
print(result)
else:
print("thank you")
how do i like repeat this test like print(do you wan to take again)
goes back to would you like to take bmu yes /no at the top
The input function returns a string.
So to get your output you need to use "float()" for height and weight:
bmi = float(weight_kg1) / (float(height_m1)** 2)
Also, you have to call your function, e.g.
bmi_calculator(name1,float(height_m1),float(weight_kg1))
After printing the result
You can add a if else condition where you can take input from the user and ask them, then accordingly you can recall your function or else exit it.
Like this:-
Take_Bmi=(input("Take bmi yes or no "))
if Take_Bmi == "yes":
def takeBMI():
name1=input(" enter your name")
height_m1=input(" enter your height in m")
weight_kg1=input(" enter your weight")
def bmi_calculator(name1,height_m1,weight_kg1):
bmi = float(weight_kg1) / (float(height_m1)** 2)
if bmi < 25 :
print(bmi)
return name1 + " not overweight"
else:
print(bmi)
return name1 + " is overweight"
result= bmi_calculator(name1,float(height_m1),float(weight_kg1))
print(result)
again=(input("Take bmi again yes or no "))
if again=="yes":
takeBMI()
else:
exit(0)
takeBMI()
else:
print("thank you")

How to remove a set of characters from a user inputted float - Python

I just started picking up python and I want to know how to do what I said in the title. The only background in programming I have is a semester long C++ class that I had in high school that I got a C in and forgot almost everything from. Here's my code:
while True:
try:
height_m = float(input("Enter your height in meters: "))
except ValueError:
print ("Please enter a number without any other characters.")
continue
else:break
while True:
try:
weight_kg = float(input("Enter your weight in kilograms: "))
except ValueError:
print ("Please enter a number without any other characters.")
continue
else:break
bmi = weight_kg / (height_m ** 2)
print ("Your bmi is",(bmi),".")
if bmi < 18.5:
print ("You are underweight.")
elif 18.5 <= bmi <=24.9:
print ("You are of normal weight.")
elif 25 <= bmi <= 29.9:
print ("You are overweight.")
else:
print ("You are obese.")
As you can see, it's just a basic BMI calculator. However, what I wanted to do was make it so that if someone were to input "1.8 m", "1.8 meters" or "1.8 ms" and the equivalent for kilograms, the program would remove the extra input and process it as if they hadn't added that. Also, any extra tips you have for me would be great. Thanks!
Replace the third line with this:
height_m = float(''.join([e for e in input("Enter your height in meters: ") if not e.isalpha()]))
It works by removing all alphabets before converting to float.
In general, this works
Height_List = []
Height = input("What is your height")
for i in Height:
if i in "1234567890.":
Height_List.append(i)
Actual_Height = float("".join(Height_List))

Not Getting Desired Result in Python Random Number Generator [duplicate]

I wrote this simple program to calculate one's BMI. But I am unable to execute it complete. Below is my program,
PROGRAM
h = input("Please Enter your height in meters:")
q = raw_input("Do you want to enter your weight in kg or lbs?")
if q=="kg":
w1 = input("Please Enter your weight in kgs:")
bmi1 = w1/(h*h)
print "Your BMI is", bmi1
if bmi1 <= 18.5:
print "Your are underweight."
if bmi1 > 18.5 & bmi1 < 24.9:
print "Your weight is normal."
if bmi1 > 25 & bmi1 < 29.9:
print "Your are overweight"
if bmi1 >= 30:
print "Your are obese"
if q=="lbs":
w2 = input("Please Enter your weightin lbs:")
bmi2 = w2/((h*h)*(39.37*39.37)*703)
print "Your BMI is:", bmi2
if bmi2<= 18.5:
print "Your are underweight."
if bmi2>18.5 & bmi2<24.9:
print "Your weight is normal."
if bmi2>25 & bmi2<29.9:
print "Your are overweight"
if bmi2>=30:
print "Your are obese"
OUTPUT
Please Enter your height in meters:1.52
Do you want to enter your weight in kg or lbs?kg
Please Enter your weight in kgs:51
Your BMI is 22.074099723
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "bmi.py", line 11, in <module>
if bmi1 > 18.5 & bmi1 < 24.9:
TypeError: unsupported operand type(s) for &: 'float' and 'float'
Where am I going wrong? Anyone just let me know..
Thanks :).
& is a bitwise operator, I think you were looking for the boolean and.
But notice that Python also supports the following syntax:
if 18.5 < bmi1 < 24.9:
# ...
Since you seemed to have trobled with indentation this is how your script might look like:
h = raw_input("Please enter your height in meters: ")
h = float(h)
w_unit = raw_input("Do you want to enter your weight in kg or lbs? ")
w = raw_input("Please enter your weight in {}: ".format(w_unit))
w = int(w)
if w_unit == "kg":
bmi = w / (h*h)
elif w_unit == "lbs":
bmi = w / ((h*h) * (39.37 * 39.37) * 703)
print "Your BMI is {:.2f}".format(bmi)
if bmi <= 18.5:
print "Your are underweight."
elif 18.5 < bmi <= 25:
print "Your weight is normal."
elif 25 < bmi < 30:
print "Your are overweight"
elif bmi >= 30:
print "Your are obese"
There are a couple of slight improvements:
The explicit conversion (since in Python 3 the input function behave like raw_input and there's nothing like the Python 2 input, it might be a good habit to write your input like that)
What really changes is the bmi value, so there's no need to write two times the same thing.
Something left to do, might be wrap the whole script into functions :)

TypeError: unsupported operand type(s) for &: 'float' and 'float'

I wrote this simple program to calculate one's BMI. But I am unable to execute it complete. Below is my program,
PROGRAM
h = input("Please Enter your height in meters:")
q = raw_input("Do you want to enter your weight in kg or lbs?")
if q=="kg":
w1 = input("Please Enter your weight in kgs:")
bmi1 = w1/(h*h)
print "Your BMI is", bmi1
if bmi1 <= 18.5:
print "Your are underweight."
if bmi1 > 18.5 & bmi1 < 24.9:
print "Your weight is normal."
if bmi1 > 25 & bmi1 < 29.9:
print "Your are overweight"
if bmi1 >= 30:
print "Your are obese"
if q=="lbs":
w2 = input("Please Enter your weightin lbs:")
bmi2 = w2/((h*h)*(39.37*39.37)*703)
print "Your BMI is:", bmi2
if bmi2<= 18.5:
print "Your are underweight."
if bmi2>18.5 & bmi2<24.9:
print "Your weight is normal."
if bmi2>25 & bmi2<29.9:
print "Your are overweight"
if bmi2>=30:
print "Your are obese"
OUTPUT
Please Enter your height in meters:1.52
Do you want to enter your weight in kg or lbs?kg
Please Enter your weight in kgs:51
Your BMI is 22.074099723
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "bmi.py", line 11, in <module>
if bmi1 > 18.5 & bmi1 < 24.9:
TypeError: unsupported operand type(s) for &: 'float' and 'float'
Where am I going wrong? Anyone just let me know..
Thanks :).
& is a bitwise operator, I think you were looking for the boolean and.
But notice that Python also supports the following syntax:
if 18.5 < bmi1 < 24.9:
# ...
Since you seemed to have trobled with indentation this is how your script might look like:
h = raw_input("Please enter your height in meters: ")
h = float(h)
w_unit = raw_input("Do you want to enter your weight in kg or lbs? ")
w = raw_input("Please enter your weight in {}: ".format(w_unit))
w = int(w)
if w_unit == "kg":
bmi = w / (h*h)
elif w_unit == "lbs":
bmi = w / ((h*h) * (39.37 * 39.37) * 703)
print "Your BMI is {:.2f}".format(bmi)
if bmi <= 18.5:
print "Your are underweight."
elif 18.5 < bmi <= 25:
print "Your weight is normal."
elif 25 < bmi < 30:
print "Your are overweight"
elif bmi >= 30:
print "Your are obese"
There are a couple of slight improvements:
The explicit conversion (since in Python 3 the input function behave like raw_input and there's nothing like the Python 2 input, it might be a good habit to write your input like that)
What really changes is the bmi value, so there's no need to write two times the same thing.
Something left to do, might be wrap the whole script into functions :)

Categories

Resources