【发布时间】:2021-09-03 01:19:14
【问题描述】:
我是 python 的初学者,只是为了测试我的技能,试图制作一个程序来检查给定的整数是否可以表示为两个素数之和,我在下面提到的代码 -
a = int(input('Enter the number: '))
def prime(b):
for i in range(2, b):
while b % i == 0:
pass
else:
return b
if a < 3:
print("The number is too short to be tested")
else:
for i in range(2, a):
for j in range(2, a):
d = prime(i)
e = prime(j)
if d + e == a:
print("It can be expressed as a sum of prime numbers")
break
else:
print("The number can not be expressed as a sum of prime numbers")
错误提示TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
我知道素数类需要先返回一个值,因此我将返回的值分别存储在变量 d 和 e 中,但徒劳无功。任何有关上述内容的帮助将不胜感激。
【问题讨论】:
-
你希望你的函数
prime()在给定参数的情况下返回什么? -
能否提供完整的错误回溯?
-
prime应该只返回True或False,您可以使用这些结果来决定是否要检查i + j == a。
标签: python