Here’s an example Python code to calculate the area of a triangle:
# Take input from the user base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) # Calculate the area of the triangle area = 0.5 * base * height # Print the area of the triangle print("The area of the triangle is", area)
In this code, we use the input()
function to prompt the user to enter the base and height of the triangle. We convert the user input to a float using the float()
function and assign them to the variables base
and height
.
Next, we calculate the area of the triangle using the formula 0.5 * base * height
and assign the result to the variable area
.
Finally, we use the print()
function to output the area of the triangle.
When you run this code, it should prompt you to enter the base and height of the triangle. Once you enter those values and press enter, it should output the area of the triangle. For example:
Enter the base of the triangle: 5 Enter the height of the triangle: 8 The area of the triangle is 20.0
Note that you can replace 5
and 8
with any other values you want to calculate the area of a triangle with a different base and height.