 # Python help

I could use some help with this code. I don’t know how to break the cycle if the triangle does not work. Any ideas?
while True:
A=(int(input(‘Length of side A:’)))
B=(int(input(‘Length of side B:’)))
C=(int(input(‘Length of side C:’)))

``````while True:
if A<0 or B<0 or C<0:
print('Error, all sides must be positive')

if (A+B)<C or (B+C)<A or (A+C)<0:
print('Error, sides do not form a traingle')

answer=(str(input('Do you wish to try again(y/n)')))
continue
else:
break

if A==B and B==C:
print('It is an equilateral triangle')
if ((A**2)+(B**2)==(C**2)) or ((B**2)+(C**2)==(A**2)) or ((A**2)+(C**2)==(B**2)):
print('It is a right triangle')
if A==B or B==C or A==C:
print('it is an isosceles triangle')
if A!=B and B!=C:
print('It is a scalene triangle')
answer=(str(input('Do you wish to try again(y/n)')))
continue
else:
break``````

I highly doubt you intended to post this under CD-Media: Photos. Wait for a moderator to move this to Python.

The rule for having a real triangle is that the sum of the two smaller sides is larger than the longest side. You can avoid having to figure out which sides are the smallest or largest by just checking all 3 possible cases and seeing if the rule holds true for them all. If it doesn’t hold for all 3, it’s not a real triangle.

Moved.

And because I’ve said it before: please put things in the correct category.

2 Likes

You could enclose everything within a `main()` function and then `return` instead of `break` to quit the function. You’ll have to call `main()` at the bottom of the program.