Learning pyhton from scratch, beginner problem with a simple calculator :)

So I started learning python via youtube, (Learn Python - Full Course for Beginners [Tutorial] - YouTube), and thougt that the best thing I can do while watching a video is to try and implement what I learn straight away.

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 :slight_smile:

Also Im new to the forum, sorry If this is in the wrong section, or im lacking etiquette.

@GerhardPeppula Welcome to the forum.

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.

Example:

print(f'Your body mass index is: {bmi}')
2 Likes

Hey GerhardPeppula :wave:
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")
2 Likes

Thanks for the answer! I will definetly research f-strings :slight_smile:
Still taking my first steps with coding, but with help from nice people on forums like this one, maybe I´ll run someday!

height=input("Enter your height: ")
weight=input(“enter weight”)
height2=float(height)*float(height)
bmi=float(weight)/height2
print("your bmi is " + str(bmi))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.