What do i write to get this program?

This is the code i used but I don’t know how to make it show me the area for the first one before asking for the second set of values.

NumOfTri = eval(input("Enter the number of triangles to calculate the area: "))

Base1 = eval(input("Enter base of triangle: "))
Height1 = eval(input("Enter height of triangle: "))

for i in range(NumOfTri - 1):
  base = eval(input("Enter base of triangle: "))
  height = eval(input("Enter height of triangle: "))

area = base * height 

print("The area of the triangle is: ", area)

Can you please elaborate on the question? I can’t seem to understand. Also, is this a school assignment?

NumOfTri = eval(input("Enter the number of triangles to calculate the area: "))
for i in range(NumOfTri):
  base = eval(input("Enter base of triangle: "))
  height = eval(input("Enter height of triangle: "))

  area = base * height # must divide by 2, this is area of square.

  print("The area of the triangle is: ", area)

basically you are on the right track, its just that you added some unnecessary code here. Also one thing to change is
for i in range(NumOfTri-1)
You do not need to -1. its unnecessary
and the formula is base*height/2

Thank me later bud

Also you can change the eval for base and height to float to handle decimals
@LamaTamr

1 Like

Don’t give the whole answer as this is probably a school assignment. We’re only allowed to point them in the right direction, not give them the answers.

2 Likes

What he wants is to evaluate the area of the triangle for the first set of values given, then followed by the second set of values.

You are probably right let me edit some stuff lolz

Even if he does submit my code to his/her teacher. She would probably question about it since its probably out of point somewhere.
since even if its a homework or sum there would be a set of questions to guide him/her.

1 Like

Its not an assignment but it was a practice sheet for my programming exam. I was trying to figure out what I did wrong to fix it before my exam. But honestly good on u for looking out cuz I get why it looks that way XD

1 Like

Thanks mate! I was tryna figure out what was wrong because I was going off some previous code examples I did which had the same premise but it wanted something completely different.

One other question. How do i get the “Done! Triangle Area program is finished” text to appear only after the n amount of triangles have been solved? I tried putting

print(“Done!..”)

at the end of the code, but it appears after the first one is solved.
Screenshot 2023-05-29 155524

How is your code formatted? Is it like:

#Number of inputs
for i in range(NumOfTri):
    #Caluclations or whatever
    print("Done!")

If so, just de-indent the last print like so:

#Number of inputs
for i in range(NumOfTri):
    #Caluclations or whatever
print("Done!")

Also, eval is a dangerous thing to use, if you want numbers with no decimals, you could do:

a = int(input("I'm a number! "))

But if you want decimals, you can:

b = float(input("I'm a number with decimals! "))
3 Likes

its currently formatted like this

NumOfTri = eval(input("Enter the number of triangles to calculate the area: "))

for i in range(NumOfTri):
  base = eval(input("Enter base of triangle: "))
  height = eval(input("Enter height of triangle: "))

  area = base * height # must divide by 2, this is area of square.

  print("The area of the triangle is: ", area)
  print("Done! Triangle Area Program is finished")

Please wrap your code in triple backticks like so:

```python
# Some code here
```

This way, the indentation is preserved, and it will also apply some syntax highlighting.

2 Likes

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