Here's my code, it seems kind of repetitive, but I couldn't figure out a way to make it differently. (It works okay btw, but it seems to me it's a bit "unclean")
while True:
try:
num1 = int(input("Type in the first parameter: "))
num2 = int(input("Type in the second parameter: "))
num3 = int(input("Type in the third parameter: "))
break
except ValueError:
print("You have to type in a number. ")
while True:
if num1 > num2 and num1 > num3:
c = num1
if c * c == num2 * num2 + num3 * num3:
print("Your triangle is a pythagorean triangle")
else:
print("Your triangle isn't a pythagorean triangle")
elif num2 > num1 and num2 > num3: # c - hypotenuse
c = num2
if c * c == num1 * num1 + num3 * num3:
print("Your triangle is a pythagorean triangle")
else:
print("Your triangle isn't a pythagorean triangle")
elif num3 > num1 and num3 > num2:
c = num3
if c * c == num2 * num2 + num1 * num1:
print("Your triangle is a pythagorean triangle")
else:
print("Your triangle isn't a pythagorean triangle")
elif num1 == num2 and num2 == num3 and num1 == num3:
print("There's no such thing as a pythagorean triangle with all sides the same, try again")
again = str(input("Do you want to continue? [Y/n]\n"))
if again == "Y" or again == "y":
pass
else:
break
-
2Why don't you just sort the numbers?Yann Vernier– Yann Vernier2018年07月22日 23:48:00 +00:00Commented Jul 22, 2018 at 23:48
-
4Since your code works, you may have better luck here: codereview.stackexchange.comsniperd– sniperd2018年07月22日 23:48:52 +00:00Commented Jul 22, 2018 at 23:48
-
As I understood, you shouldn't 2 loops. Place the code block of 2nd while loop inside the try block of first while loop (after the existing code block), finally use continue statement in place of pass. That makes sense.hygull– hygull2018年07月23日 00:19:21 +00:00Commented Jul 23, 2018 at 0:19
2 Answers 2
You can use the max function and a bit of math to reduce the need for if cases. Example below without the loops for repetition
num1 = int(input("Type in the first parameter: "))
num2 = int(input("Type in the second parameter: "))
num3 = int(input("Type in the third parameter: "))
if num1 == num2 == num3:
print("There are no pythagorean triangle with all sides equal")
exit(1)
c= max(num1,num2,num3)
if c * c == num1 * num1 + num2 * num2 + num3 * num3 - c * c:
print("Your triangle is a pythagorean triangle")
else:
print("Your triangle isn't a pythagorean triangle")
answered Jul 23, 2018 at 0:39
visibleman
3,3251 gold badge16 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Instead of writing num1 > num2 and num1 > num3 you may easily do num2 < num1 > num3 and it will work just the same.
On the other hand I'd sort the numbers and don't worry about different combinations:
num1, num2, num3 = sorted( [num1, num2, num3] )
# here the num1 < num2 < num3 so you may use a single check
if num1 * num1 + num2 * num2 == num 3 * num3 : # etc...
answered Jul 23, 2018 at 0:04
lenik
23.6k4 gold badges38 silver badges44 bronze badges
Comments
lang-py