So I tried to write a simple program that calculates your BMI.
The program worked ok, but I couldn’t manage to add a string to the result that says (here is the result) “: is your BMI”
Its probably a very obvious mistake im making, but Im very new to pyhton, or any programming language.
Heres the code;
height = input("Your height in meters (1.72 for example) ")
weight = input("Your weight ")
height2 = float(height) * float(height)
bmi = (float(weight) / float(height2))
print(str(bmi)) + (“is your bmi”)
I’d appreciate if someone told me what is my major malfunction here, the problem is in how Im trying to involve the string at the end, if I remove it all is fine. But I’d like to know how to add a string there
Also Im new to the forum, sorry If this is in the wrong section, or im lacking etiquette.
You cannot join a print statement to a string as I see in you example code. The text is your bmi should be within the print statement. Do some research on f-strings. They are a pretty nice way to print output to the screen.
Hey GerhardPeppula
As brandon_wallace stated above, f-strings are a neat way to format strings to contain values from variables.
You could use f-strings in the following way-
a = 0
print(f'this is a string, value of variable a - {a}')
# This gets rendered as - "this is a string, value of a - 0"
Mainly, once you prepend an f to the string, you could get values from variables in there by wrapping the variable name in {} inside the string.
→ Here is why the method you tried, gave errors.
According to the way you were trying to print this-
print(str(bmi)) + (“is your bmi”)
This wouldn’t work since print() takes a string or multiple strings as arguments, inside the brackets.
You could make this work by putting both the strings in the function as multiple arguments-
print(str(bmi), "is your bmi")
You could also use this-
print(str(bmi) + " " + "is your bmi")
print(str(bmi) + " is your bmi")
Thanks for the answer! I will definetly research f-strings
Still taking my first steps with coding, but with help from nice people on forums like this one, maybe I´ll run someday!