#🔒 I need someone to check my code

15 messages · Page 1 of 1 (latest)

ruby kernel
#

I do not know how to check if this is working properly.

def find_pythagorean_triples(max_hypotenuse):
    triples = []
    for a in range(1, max_hypotenuse):
        for b in range(a, max_hypotenuse):
            c = (a**2 + b**2) ** 0.5
            if c.is_integer() and c < max_hypotenuse:
                triples.append((a, b, int(c)))
    return triples

try:
    max_hypotenuse = int(input("Enter the maximum hypotenuse value: "))
    if max_hypotenuse <= 0:
        raise ValueError("The maximum hypotenuse must be a positive integer.")
    triples = find_pythagorean_triples(max_hypotenuse)
    for triple in triples:
        print(triple)
except ValueError as e:
    print(f"Invalid input: {e}")

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

def is_primitive(triple):
    a, b, c = triple
    return gcd(gcd(a, b), c) == 1

primitive_triples = [triple for triple in triples if is_primitive(triple)]
for triple in primitive_triples:
    print(triple)
    print("Primitive Pythagorean triples with hypotenuse less than 300:")
    print(primitive_triples)
crisp daggerBOT
#

@ruby kernel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ruby kernel
#

its supposed to determine all of the Pythagorean triples with hypotenuse less than a defined number

maiden solar
#

Are you familiar with unit testing? You could try writing a test for the find_pythagorean_triples function

#

Basically, run the function with a certain input, and when you get the output back, assert that the output is what you expected

harsh bronze
#

The OP could start with some asserts:

assert find_pythagorean_triples(3) == [ list of expected triples here ... ]
#

Before getting into writing a full on test suite.

#

Some random remarks:

  • define all your functions above any of the inline top level code, it ensures you don't accidentally call a function before it exists
  • you've got 300 in your print() down the bottom, should that be max_hypotenuse instead?
  • you go range(1,max_hypotenuse) but your input() prompt suggests the max_hypotenuse is included - range() stops one short - you need to decide on < or <= and make that clear in the prompt
#

@ruby kernel ^^

ruby kernel
#

Oh thank you

silver glade
#

uh cameron what is ur job

#

like r u a dev

harsh bronze
#

dev/sysadmin kind of thing.

crisp daggerBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.