编写确定输入整数是否为完美数的程序((using for statement) 完美数是指所有除数本身之和与自己相同的数字 For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect
3 回答
牧羊人nacy
TA贡献1862条经验 获得超7个赞
python的字符串类型有一个属性isdigit可以判断是否为数字假设设置了一个变量a,则选择把a转换成字符串,再判断代码如下:a=123print(str(a).isdigit())如果变量a是数字,则返回True,否则返回False
jeck猫
TA贡献1909条经验 获得超7个赞
num=int(input('Type the number :'))
rag=[x for x in range(1,num//2 +1) if not num % x]
if sum(rag)==num:
print (str(num) +' is the perfect number')
else:
print (str(num) + ' is not the perfect number')
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
n_num=int(input('Input a numer:'))
while n_num>1:
if sum(filter(lambda x:n_num%x==0,range(n_num)[1:]))==n_num:
print(' {} is a perfect'.format(n_num))
else:
print(' {} not perfect'.format(n_num))
n_num=int(input('Input a numer:'))
- 3 回答
- 0 关注
- 263 浏览
添加回答
举报
0/150
提交
取消
