0

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
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Jul 22, 2018 at 23:44
3
  • 2
    Why don't you just sort the numbers? Commented Jul 22, 2018 at 23:48
  • 4
    Since your code works, you may have better luck here: codereview.stackexchange.com Commented 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. Commented Jul 23, 2018 at 0:19

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.