小能豆

How do I concatenate a float or a interger?

python

I’m a very beginner and know only the very basic syntax. I am trying to make a circumference calculator, with the user inputting the radius. This is what I have right now, but I want to be able to have a text answer like “The circumference of this circle is ____”. Right now I can only output the answer.

rad = input("Enter the radius of circle: ")
radius = float(rad)
circumference = 2 * 3.14 * radius
print(circumference)

阅读 100

收藏
2023-11-27

共1个答案

小能豆

You’re on the right track! To include a text answer along with the calculated circumference, you can use the print statement to display a formatted message. Here’s an example:

rad = input("Enter the radius of the circle: ")
radius = float(rad)
circumference = 2 * 3.14 * radius

# Format the output message
message = "The circumference of this circle with radius {} is {:.2f}".format(radius, circumference)

# Print the message
print(message)

In this example, the format method is used to create a string where {} is a placeholder for the radius value, and {:.2f} is a placeholder for the circumference value with two decimal places. The format method then replaces these placeholders with the actual values before printing the message.

Now, when you run the program and input the radius, it will print a message like “The circumference of this circle with radius [radius] is [calculated circumference]”.

2023-11-27