发布网友 发布时间:2022-03-03 23:02
共1个回答
热心网友 时间:2022-03-04 00:31
def is_prime(n):
"""判断一个正整数是否为素数"""
if isinstance(n, (int, float)):
try:
for i in range(2, n):
if n % i == 0:
return False
return True
except Exception as ex:
return ex
else:
return False
def prime_sum(alist):
return sum(alist)
if __name__ == "__main__":
astr = '56,41,70,31,83'
num = [int(i) for i in astr.split(',')]
alst = []
print('其中质数位:', end= '')
for i in num:
if is_prime(i):
alst.append(i)
print(i, end = ',')
print('质数之和为:{}'.format(prime_sum(alst)))