Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Modified code def triangle_type(a, b, c): ''' Return a string indicating the type of triangle (Equilateral, Isosceles, Scalene, Impossible) '''

Modified code

def triangle_type(a, b, c):
  '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 if a == c:
 return 'Equilateral'
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()

Further improvement

#Further improvement UseUse the doctest module to write the tests:

#Modified code def triangle_type(a, b, c): ''' Return a string indicating the type of triangle (Equilateral, Isosceles, Scalene, Impossible) '''

 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 if a == c:
 return 'Equilateral'
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()

#Further improvement Use the doctest module to write the tests:

Modified code

def triangle_type(a, b, c):
  '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 if a == c:
 return 'Equilateral'
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()

Further improvement

Use the doctest module to write the tests:

Tidy the if/else chain
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
a, b, c = sorted([a, b, c])
if a + b <= c:
 # N.B. automatically catches a < 0, since b <= c
 return 'Impossible'
if a != b != c:
 return 'Scalene'
elif a == c:
 return 'Equilateral'
else:
 return 'Isosceles'
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elifif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elifif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
a, b, c = sorted([a, b, c])
if a + b <= c:
 return 'Impossible'
if a != b != c:
 return 'Scalene'
elif a == c:
 return 'Equilateral'
else:
 return 'Isosceles'
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
a, b, c = sorted([a, b, c])
if a + b <= c:
 # N.B. automatically catches a < 0, since b <= c
 return 'Impossible'
if a != b != c:
 return 'Scalene'
elif a == c:
 return 'Equilateral'
else:
 return 'Isosceles'
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 if a == c:
 return 'Equilateral'
 return 'Isosceles'
 
def main():
 a = input("Length of side a: ")
 b = input("Length of side b: ")
 c = input("Length of side c: ")
 print(f"({a}, {b}, {c}) is a {triangle_type(a, b, c)} triangle")
if __name__ == "__main__":
 main()
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 if a == c:
 return 'Equilateral'
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
Add a test for obtuse isoceles
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
def triangle_type(a, b, c):
 '''
 Return a string indicating the type of triangle
 (Equilateral, Isosceles, Scalene, Impossible)
 >>> triangle_type(1, 1, 2)
 'Impossible'
 >>> triangle_type(-1, -1, -1)
 'Impossible'
 >>> triangle_type(1, 1.0, 1)
 'Equilateral'
 >>> triangle_type(1, 2, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 2)
 'Isosceles'
 >>> triangle_type(2, 3, 4)
 'Scalene'
 '''
 a, b, c = sorted([a, b, c])
 if a + b <= c:
 return 'Impossible'
 if a != b != c:
 return 'Scalene'
 elif a == c:
 return 'Equilateral'
 else:
 return 'Isosceles'
if __name__ == "__main__":
 import doctest
 doctest.testmod()
added 970 characters in body
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /