python 3中的平方因子分解?
注意-如果您想要更新代码,请转到编辑,如果您想要查看原始问题,请查看此处。但是,我已经对代码进行了编辑,因此此代码块中的错误可能已经过时python 3中的平方因子分解?,python,python-3.x,Python,Python 3.x,注意-如果您想要更新代码,请转到编辑,如果您想要查看原始问题,请查看此处。但是,我已经对代码进行了编辑,因此此代码块中的错误可能已经过时 作为一名自学成才的Python3程序员,我一直在研究简化激进表达式的方法。然而,程序中不起作用的部分是平方分解。我有一个检测平方数的函数,它工作得很好。不起作用的功能如下- def sqfactorslist(num): factors = [] counter = num // 2 + 1 whil
作为一名自学成才的Python3程序员,我一直在研究简化激进表达式的方法。然而,程序中不起作用的部分是平方分解。我有一个检测平方数的函数,它工作得很好。不起作用的功能如下-
def sqfactorslist(num):
factors = []
counter = num // 2 + 1
while counter > 0:
if counter == 1:
factors.append(num) #if no square ints other than 1 square into num, append and finish.
while is_square(counter) == True and counter != 1: #If counter is a valid square integer, append it's square and subtract it from num.
if (counter ** 2) >= num:
factors.append(counter ** 2)
num -= counter ** 2
else: #Else, continue with a program.
break
if counter > 0:
counter -= 1 #If counter more than 0, subtract 1 from it
else:
break #If number is equal to or less than 0, break
return factors #If the input is 32, this should return 16 and 2.
它只返回一个无限循环。有人知道怎么了吗
编辑1——我已经更改了程序,使其能够运行,但现在我遇到了一个奇怪的问题:如果我输入一个平方数作为num,例如16,我会在返回列表中得到一个大于输入值(e.x.81)的数字。对于非平方数,我没有得到返回的元素。 为了让它运行,我在第二个while循环结束后缩进了第一个if语句
编辑2——我再次更改了程序,但我提出了另一个类似于上述问题的问题。在消除了另一个错误之后,在第二个while循环中,我发现平方数已经显示为平方,程序现在有了一个新问题-如果我使用非平方整数,它返回一个空列表,如果我使用平方整数,比如16,它会给我一个无限循环。当前代码如下所示--
当你得到一个无限循环时,你为
num
输入什么?而is_square(计数器)是你的无限循环。它永远不会退出,只返回无限循环。有人知道出了什么问题吗?
听起来是抛出pdb
in@Kh40tiK,PDB?我查看了python.org,并按照建议使用了它,但得到了这个,重新表述,使用步骤-(1)(->步骤->--返回-(1)(->)步骤->(pdb路径)self.quitting=True->我建议您阅读并阅读。当你(彻底地)改变你的问题时,你可以考虑开始一个新的问题,因为它相当混乱,用当前的变化目标去解释实际问题是什么,
def findsqfactors(num):
factors = []
counter = num // 2 + 1
while counter > 0:
if counter == 1:
factors.append(num) #If no square ints other than 1 square into num, append then finish.
while is_square(counter) == True and counter != 1: #
if counter >= num:
factors.append(counter)
num -= counter
else:
break
if counter > 0:
counter -= 1
else:
break
return factors