Skip to main content
 首页 » 编程设计

python之无法检查数字是否完美

2025年05月04日15shangdawei

我正在尝试学习 python,但遇到了错误。

所以,这是我要运行的程序的代码:检查数字是否为完美数字:

total=0 
x=0 
number=input('Enter a number to be checked whether it is perfect or not:') 
number=int(number) 
for divisor in range(1,number+1): 
    x=number%divisor      
if x==0: 
    total+=x 
else: 
    print('The number' +str(x)+'is not a divisor of' +str(number)) 
 
print('\n'+ 'The sum is:' +str(total)) 
 
if number==total: 
    print('The number' +str(number) + 'is a perfect number') 
else: 
    print('The number' +str(number) +'is not a perfect number') 

请您参考如下方法:

是时候...

正在调试!!!

我将你的代码粘贴到一个文件中,然后运行它:

$ python3 p.py  
Enter a no to be checked whether it is perfect or no:6 
 
The sum is:0 
The no6is not a perfect no 

嗯,6 是一个完美的数字,那是怎么回事呢?

缩进错误

阅读您的代码,我感觉下面的 if 应该在 for 的每个值上运行,对:

for divisor in range(1,number+1): 
    x=number%divisor      
if x==0: 
    total+=x 
else: 
    print('The no' +str(x)+'is not a divisor of' +str(number)) 

这不会发生,因为缩进是错误的!要在 for 中,if 应该在属性的同一行:

for divisor in range(1,number+1): 
    x=number%divisor      
    if x==0: 
        total+=x 
    else: 
        print('The no' +str(x)+'is not a divisor of' +str(number)) 

奇怪的信息

那么,让我们再试一次:

$ python3 p.py  
Enter a no to be checked whether it is perfect or no:6 
The no2is not a divisor of6 
The no1is not a divisor of6 
 
The sum is:0 
The no6is not a perfect no 

嗯,还是错了。

奇怪的是,它说 2 和 1 不能整除 6。荒谬!但这实际上是 print() 调用的问题:

    print('The no' +str(x)+'is not a divisor of' +str(number)) 

x 不是除数——x 是除数!

好吧,你搞砸了变量。当有很多变量时这是正常的……而且它们没有有意义的名称。然后我们将 x 重命名为 rest:

for divisor in range(1,number+1): 
    rest=number%divisor      
    if rest==0: 
        total+=rest 
    else: 
        print('The no' +str(rest)+'is not a divisor of' +str(number)) 

当然打印的信息是错误的:我想显示除数,而不是其余的!让我们改变一下:

    print('The no' +str(divisor)+'is not a divisor of' +str(number)) 

有意义的名字有什么帮助

现在出现了一些有趣的事情:我们将 rest 添加到总数中......

    if rest==0: 
        total+=rest 

...但是我们应该加上除数!让我们纠正一下:

for divisor in range(1,number+1): 
    rest=number%divisor      
    if rest==0: 
        total+=divisor 
    else: 
        print('The no' +str(divisor)+'is not a divisor of' +str(number)) 

终于!现在程序可以正常工作了……还是可以?

$ python3 p.py  
Enter a no to be checked whether it is perfect or no:6 
The no4is not a divisor of6 
The no5is not a divisor of6 
 
The sum is:12 
The no6is not a perfect no 

该死!

范围限制

为什么说和是12呢?好吧,让我们再看看循环:

for divisor in range(1,number+1): 

哦,现在我明白了......让我们称之为打印数字:

>>> number = 6 
>>> for divisor in range(1,number+1): 
...     print(divisor) 
...  
1 
2 
3 
4 
5 
6 

就是这样!当您运行从 1 到 number+1(在本例中为 7)的循环时,将处理数字本身 (6)。好吧,6 是 6 的约数,所以它也被加到总数中……复制它!所以你应该只走到数字:

for divisor in range(1,number): 

结论

有用吗?在此处查看输出:

$ python3 p.py  
Enter a no to be checked whether it is perfect or no:6 
The no4is not a divisor of6 
The no5is not a divisor of6 
 
The sum is:6 
The no6is a perfect no 
 
$ python3 p.py 
Enter a no to be checked whether it is perfect or no:28 
... 
The no27is not a divisor of28 
 
The sum is:28 
The no28is a perfect no 
 
$ python3 p.py 
Enter a no to be checked whether it is perfect or no:9 
The no2is not a divisor of9 
... 
The no8is not a divisor of9 
 
The sum is:4 
The no9is not a perfect no 

是的!

我想您真的很难看到所有问题,因为问题太多了。秘诀是:学会每次看一个问题。