<分区>
我有下面的代码,它打印给定数字的唯一因子组合。 但我没有给出所需的输出。代码如下:
#Print the factors list for the given number
def print_factors_list(dividend, factorstring, predivisor):
"""This function takes a number and prints the factors"""
divisor = dividend - 1
for i in range(divisor, 1, -1 ):
if dividend % i != 0:
continue
if i > predivisor:
continue
quotient = dividend / i
if quotient <= i:
if quotient <= predivisor:
print factorstring + str(i) + "*" + str(quotient)
print_factors_list(quotient, str(factorstring) + str(i) + "*", i)
#Check if the number is greater than 0
def print_factors(x):
# Check if the number is greater than 0
if (x < 0):
print "Enter a positive interger"
#Go to the function print_factors_list for further evaluation
else:
print_factors_list(x, str(x) + "*" + str(1) + "\n", x )
#Take input from the user
num = int(input("Enter a number: "))
print_factors(num)
我得到如下输出:
32*1
16*2
32*1
8*4
32*1
8*2*2
32*1
4*4*2
32*1
4*2*2*2
32*1
2*2*2*2*2
我应该得到
32*1
16*2
8*4
8*2*2
4*4*2
4*2*2*2
2*2*2*2*2
我是 python 的新手,所以犯了一些愚蠢的错误。请有人帮助我在逻辑上出错的地方。谢谢。