Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “问题”;而";循环_Python_Loops_While Loop - Fatal编程技术网

Python “问题”;而";循环

Python “问题”;而";循环,python,loops,while-loop,Python,Loops,While Loop,我必须以某种方式做一个函数,在这个函数中,当我执行它时,我将能够添加一个数字的所有除数,如下所示 这让我发疯了,我已经陷入同样的问题大约一个小时了 def sum_divisors(n): # Return the sum of all divisors of n, not including n divisor = 1 while divisor < n: if n%divisor==0: return divisor divisor = di

我必须以某种方式做一个函数,在这个函数中,当我执行它时,我将能够添加一个数字的所有除数,如下所示

这让我发疯了,我已经陷入同样的问题大约一个小时了

def sum_divisors(n):
  # Return the sum of all divisors of n, not including n
  divisor = 1
  while divisor < n:
    if n%divisor==0:
      return divisor
      divisor = divisor + 1
    else:
      divisor = divisor + 1

print(sum_divisors(6)) # Should be 1+2+3=6
print(sum_divisors(12)) # Should be 1+2+3+4+6=16
def和除数(n):
#返回n的所有除数之和,不包括n
除数=1
除数
这很奇怪:

if n%divisor==0:
      return divisor
      divisor = divisor + 1 //<= this is actually dead code, since is after the return statement...
工作起来更像一个计数器,但你错过了累加器

你应该这样做

accum = 0
while divisor < n:
    foo = n % divisor
    if foo == 0:
    accum = accum + divisor
accum=0
除数
这很奇怪:

if n%divisor==0:
      return divisor
      divisor = divisor + 1 //<= this is actually dead code, since is after the return statement...
工作起来更像一个计数器,但你错过了累加器

你应该这样做

accum = 0
while divisor < n:
    foo = n % divisor
    if foo == 0:
    accum = accum + divisor
accum=0
除数
在函数中,找到除数后立即返回。 这就是你的建议不起作用的原因 尝试将每个n%除数==0放入一个列表,并在while结束时返回它


或者直接打印出来。

在您的函数中,找到除数后立即返回。 这就是你的建议不起作用的原因 尝试将每个n%除数==0放入一个列表,并在while结束时返回它


或者试着直接打印出来。

一个简单的谷歌搜索会让你找到有很多解释的答案:

如果您考虑一些效率:

我们需要检查除数直到一个数的
sqrt

import math 
def sum_divisors(num) : 

    # Final result of summation of divisors 
    result = 0

    # find all divisors which divides 'num' 
    i = 2
    while i<= (math.sqrt(num)) : 

        # if 'i' is divisor of 'num' 
        if (num % i == 0) : 

            # if both divisors are same then 
            # add it only once else add both 
            if (i == (num / i)) : 
                result = result + i; 
            else : 
                result = result +  (i + num//i); 
        i = i + 1

    # Add 1 to the result as 1 is also  
    # a divisor 
    return (result + 1); 

print(sum_divisors(6))
print(sum_divisors(12))
导入数学
定义和除数(num):
#除数求和的最终结果
结果=0
#查找除“num”的所有除数
i=2

而我一个简单的谷歌搜索会让你找到有很多解释的答案:

如果您考虑一些效率:

我们需要检查除数直到一个数的
sqrt

import math 
def sum_divisors(num) : 

    # Final result of summation of divisors 
    result = 0

    # find all divisors which divides 'num' 
    i = 2
    while i<= (math.sqrt(num)) : 

        # if 'i' is divisor of 'num' 
        if (num % i == 0) : 

            # if both divisors are same then 
            # add it only once else add both 
            if (i == (num / i)) : 
                result = result + i; 
            else : 
                result = result +  (i + num//i); 
        i = i + 1

    # Add 1 to the result as 1 is also  
    # a divisor 
    return (result + 1); 

print(sum_divisors(6))
print(sum_divisors(12))
导入数学
定义和除数(num):
#除数求和的最终结果
结果=0
#查找除“num”的所有除数
i=2
而i
def和除数(n):
总和=0
z=1
当n>z时:
如果n%z==0:
sum=sum+z
z=z+1
其他:
z=z+1
#返回n的所有除数之和,不包括n
回报金额
定义和除数(n):
总和=0
z=1
当n>z时:
如果n%z==0:
sum=sum+z
z=z+1
其他:
z=z+1
#返回n的所有除数之和,不包括n
回报金额
第5行应该有一个if条件,以便也能获得0的所需结果

第5行应该有一个if条件,以便也能获得0的所需结果

定义和除数(n):

total=[0]
除数=1
当除数
定义和除数(n):

total=[0]
除数=1
当除数
您可以使用这个简单的while循环打印一个数字的所有除数之和。你应该使用累加器来增加温度

def sum_divisors(n):
  sum = 0
  accum = 1
  while n != 0 and accum < n:
    if n % accum == 0:
      sum += accum
    accum += 1
  return sum



print(sum_divisors(6)) # prints 6
print(sum_divisors(12)) # prints 16
def和除数(n):
总和=0
累计=1
而n!=0和accum
您可以使用这个简单的while循环打印一个数字的所有除数之和。你应该使用累加器来增加温度

def sum_divisors(n):
  sum = 0
  accum = 1
  while n != 0 and accum < n:
    if n % accum == 0:
      sum += accum
    accum += 1
  return sum



print(sum_divisors(6)) # prints 6
print(sum_divisors(12)) # prints 16
def和除数(n):
总和=0
累计=1
而n!=0和accum
定义和除数(n):
总和=0
累计=1
#返回n的所有除数之和,不包括n
而n!=0和accum
定义和除数(n):
总和=0
累计=1
#返回n的所有除数之和,不包括n
而n!=0和accum
定义和除数(n):
总和=0
x=1
而n!=0和x
定义和除数(n):
总和=0
x=1
而n!=0和x
定义和除数(n):
x=1
a=0
而n=0和x
def和除数(n):
x=1
a=0

而n=0和xLet我猜,问题是这个代码是无法访问的?(应该是你说的,而不是我)。我认为这是一个传统,你也应该投票给你接受的答案。让我猜猜,问题是这个代码是不可访问的?(应该是你说的,而不是我)。我认为这是一个传统,你也应该投票给你接受的答案。这取决于他的需要,如果这只是一个检查与否的问题,但我同意这取决于他的需要,如果这只是一个检查与否的问题,但我同意这可能会回答这个问题,哟
def sum_divisors(n):
  sum = 0
  x = 1
  while n != 0 and x < n :
      
    if n % x == 0  :
      sum += x
    else:
      sum += 0
    x += 1    
  return sum
def sum_divisors(n):
    x = 1
    a = 0
    while n!=0 and x<n:
        if n%x == 0:
        a = a + x
        x += 1  
    #Return the sum of all divisors of n, not including n
    return a

print(sum_divisors(0)) #0

print(sum_divisors(3)) # Should sum of 1

print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18

print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51