Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:09:28+00:00 2026-06-01T09:09:28+00:00

[community edit: original title was python conditionals, OP is asking what is wrong with

  • 0

[community edit: original title was “python conditionals”, OP is asking what is wrong with code below]

I made a function that’s supposed to determine if three sides can theoretically form a triangle. It works fine in my opinion , but when i input the code in pyschools.com website, it tells that in some test cases it doesn’t work (unfortunately it doesn’t show me the cases it didn’t work). Is something missing from my code, so in some special cases my logic breas down? Thank you very much for your help.
Here’s the function:

import math
def isTriangle(x, y, z):
    if x > 0 and y > 0 and z > 0:
        if x > y and x > z:
            c = x
        elif y > x and y > z:
            c = y
        else:
            c = z
        if c == math.sqrt(x**2 + y**2):
            return True
        else:
            return False
    else:
        return False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:09:29+00:00Added an answer on June 1, 2026 at 9:09 am

    It is easier to just do:

    def isTriangle(sides):
        smallest,medium,biggest = sorted(sides)
        return smallest+medium>=biggest and all(s>0 for s in sides)
    

    (edit: I have decided to say that 2,2,4 is technically a triangle, but a degenerate triangle; change >= to > if you do not consider it to be a triangle.)

    This is exactly what you’re doing. You are calculating c = largest = max(x,y,z) correctly, but then doing return math.sqrt(x**2+y**2) which checks if it is a right triangle.

    Demo:

    >>> isTriangle([2,2,6])
    False
    >>> isTriangle((5,5,9))
    True
    >>> isTriangle([-1,2,2])
    False
    

    Below I mention how your code can be simplified:

    import math               # from math import * for such common functions
    def isTriangle(x, y, z):  # better to pass in a tuple or object, but this works
        if x>0 and y>0 and z>0:  # (then you could do all(s>0 for s in sides))
                                 # (you could also do isTriangle(*sides))
                                 # (you might need to add checks len(sides)==3
                                 #  if your input data might include e.g. squares)
            if x > y and x > z:    # \
                c = x              #  |
            elif y > x and y > z:  #  > This is the same as c = max(x,y,z)
                c = y              #  |
            else:                  #  |
                c = z              # /
            if c == math.sqrt(x**2 + y**2): # \
                return True                 #  | Same as return c==sqrt(x**2+y**2)
            else:                           #  |  
                return False                # /   
        else:
            return False
    

    “if bool return True else return False” is the SAME as “return bool” in any almost any modern programming language. The former is unnecessarily verbose and should never be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You may have noticed that we now show an edit summary on Community Wiki
EDIT: Yields that there is already new PHP framework written as extension. http://code.google.com/p/yafphp ,
EDIT: It turns out that the problem was with an input parm. Apparently, if
The user can edit his account and has a menu to choose his community
I am developing a website to extend community server, and i found that community
The question is in the title... I searched but couldn't find anything. Edit: I
I noticed that some of my gzip decoding code seemed to be failing to
Under VoltDB Don'ts http://community.voltdb.com/DosAndDonts They state Don't create queries that return large volumes of
I have seen the prior questions and answers. In that one question, the original
Edit: This makes alot more sense to me now that i've taken a step

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.