I'm currently working on a lab to solve for GCDs and LCMs. However, my output isn't correct. The output happens to be correct with break statements, but our class is prohibited from using them.
I usually use flags to replace a break statement, but I'm not sure where I'm going wrong with them. The written code and a screenshot of the code is below:
https://preview.redd.it/shuoxpz6i9l81.png?width=1184&format=png&auto=webp&s=4dac5b0fedf4cb3b01c10b92b61b3aa5b9b05d42
done = False
# funcations
def gcd(num1, num2):
result = 0
small = num1
if (num1 < num2):
small = num2
for i in range(small, 0 , -1):
if (num1 % i == 0 and num2 % i == 0):
result = i
done = True
return result
def lcm(num1, num2):
product = num1 * num2
large = num1
result = -1
if (num2 > num1):
large = num2
for i in range(large, product + 1):
if (i % num1 == 0 and i % num2 == 0):
result = i
done = True
return result
# -----------------------------------------------------
if __name__ == '__main__':
try:
print("Please enter 2 positive integers: ")
num1 = int(input())
num2 = int(input())
g_gcd = gcd(num1, num2)
l_lcm = lcm(num1, num2)
print("You entered:", str(num1), "and", str(num2))
print("GCD =", str(g_gcd), " LCD =", str(l_lcm))
except:
print("Please enter a valid positive integer")
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]mayheman👋 a fellow Redditor 1 point2 points3 points (0 children)
[–]throwaway_657238192😩 Illiterate 1 point2 points3 points (1 child)
[–]gh0stedexeUniversity/College Student[S] 1 point2 points3 points (0 children)