为什么用python求两数间素数和会输出一堆等于号?
def num(n): for i in range(2,n):
if n %i == 0:
return 0
break
else:
return n
a = int(input())
b = int(input())
s = 0
for n in range(a,b+1):
s += num(n)
if n == 1:
print("he%d"%s-1)
else:
print("he%d"%s)
回答:
因为函数每次调用只能有一个返回值,换成列表再计算吧!
python">def num(n): return [n if n%i!=0 else (n-n) for i in range(2,n)]#条件if else生成列表推导式
a = int(input())
b = int(input())
s = 0
for n in range(a,b+1):
for t in num(n):#此处原来返回none,none与int不能运算
s += t#拆开返回的列表推导式进行运算
if n == 1:
print("he%d"%(s-1))#不同数据类型不能运算,先运算再合并字符串
else:
print("he%d"%s)
回答:
def num(n): for i in range(2,n):
if n %i == 0:
return 0
else:
return n
# a = int(input())
# b = int(input())
a = 10
b = 50
s = 0
result = []
for n in range(a,b+1):
if(num(n) != 0):
result.append( num(n))
s += num(n)
if __name__ == '__main__':
print(result)
print(s)
以上是 为什么用python求两数间素数和会输出一堆等于号? 的全部内容, 来源链接: utcz.com/p/938692.html