如何避免Python中的语义错误?
我的家庭作业是关于费马的最后一个定理: 1) 编写一个名为check_fermat的函数,它接受四个参数-a、b、c和n-并检查fermat定理是否成立 2) 编写一个函数,提示用户输入a、b、c和n的值,并将其转换为 整数,并使用checku fermat检查它们是否违反fermat定理 下面是我的代码。它起作用了。 但是,在正确答案后面总是有一个“无”,例如“正确!无” 如果你能帮我解决这个问题,我将不胜感激。非常感谢你如何避免Python中的语义错误?,python,Python,我的家庭作业是关于费马的最后一个定理: 1) 编写一个名为check_fermat的函数,它接受四个参数-a、b、c和n-并检查fermat定理是否成立 2) 编写一个函数,提示用户输入a、b、c和n的值,并将其转换为 整数,并使用checku fermat检查它们是否违反fermat定理 下面是我的代码。它起作用了。 但是,在正确答案后面总是有一个“无”,例如“正确!无” 如果你能帮我解决这个问题,我将不胜感激。非常感谢你 def check_fermat(a,b,c,n): if n
def check_fermat(a,b,c,n):
if n > 2:
print("“Holy smokes, Fermat was wrong!")
if n == 2 and a**n + b**n == c**n:
print("Correct!")
else:
print("No, that doesn’t work")
def check_number():
a = int(input("Choose a number for a: "))
b = int(input("Choose a number for b: "))
c = int(input("Choose a number for c: "))
n = int(input("Choose a number for n: "))
print(check_fermat(a,b,c,n))
check_number()
这一行是打印None
,这是因为check\u fermat的返回值是None
解决办法是:
- 仅在check_fermat中打印,从
打印中删除打印(check_fermat(a、b、c、n))
- 或者,从check\u fermat返回一个字符串(或其他一些合理的返回值),并保持打印(check\u fermat(a,b,c,n))不变
print(check\u fermat(a,b,c,n))
打印None,因为check\u fermat
返回None,只需执行check\u fermat(a,b,c,n)
您可能还需要查看其副本和相关问题。或者更好的是,让函数返回字符串。但恐怕你的职能还有比这更糟糕的问题。
print(check_fermat(a,b,c,n))