【发布时间】:2021-11-03 06:24:50
【问题描述】:
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)
【问题讨论】:
-
也许更改错误消息可能会有所帮助:
raise ValueError("Invalid entry")
标签: python python-3.x python-2.7 try-catch try-except