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)')))
    if answer=='y':
        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)')))
if answer=='y':
    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.

Please at least be specific that this is help with your homework in your post.

Secondly, you need to qualify what you mean by does not work.

To learn how to program is like learning a new language, with new grammar rules, and a new protocol of social cues. So, you can start to map out logically what is supposed to happen, and then just write code to match.

So, if by Does not work you mean the user inputs have violated the first triangle inequality where the sum of the two sides is less than the third side, then it would make sense for your code to figure out how to know which of the two sides are the smallest, then add those together to see if they are larger than the longest side.

There are a lot of ways to do that, some more elegant than others. You should probably use the class instruction to know how to solve your problem. For example, given that you have some simple numerical inputs, here is a link to some python documentation that would probably help you finish that part.

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