Reputation: 1
import math
def coprimeTestFunction(val1, val2):
if val1 < 0 or val2 < 0:
raise ValueError
if type(val1) != int or type(val2) != int:
raise ValueError
result = math.gcd(val1, val2)
if result == 1:
print('The values {0} and {1} are coprime'.format(int1, int2))
elif result != 1:
print('The values {0} and {1} are not coprime, they have a GCD of {2} '.format(int1, int2, result))
else:
print('We have encountered an error')
try:
int1 = int(input('Enter first integer: '))
int2 = int(input('Enter second integer: '))
except ValueError:
print('Invalid entry')
coprimeTestFunction(int1, int2)
Upvotes: 0
Views: 38
Reputation: 737
It seems that your code is a bit misplaced. Your try/except code should include where you have called the function. E.g.:
try:
int1 = int(input('Enter first integer: '))
int2 = int(input('Enter second integer: '))
coprimeTestFunction(int1, int2)
except ValueError:
print('Invalid entry')
That should work. The reason behind this is because your inputs don't actually check whether the numbers are above 0, or an integer. The function does. So by leaving the function call outside the loop, the error isn't caught.
Upvotes: 1