我正在工作的功能应该告诉用户他们给出的数字是否是完美数字(即等于它的因子总和的一半)。如果用户给出数8,输出应该是这样的:我怎样才能返回一个在Python中的字符串中更改int?
8 is not a perfect number
,但我想不出该怎么把在return语句,使int,它的变化取决于用户的输入,打印与字符串。到目前为止的代码看起来是这样的:
#the代码是另一大功能是,如果我是这样做的回报(NUM1为ELIF
elif(message == 2):
num1 = int(input("""Please enter a positive integer :"""))
while(num1 <= 0):
print("Number not acceptable")
num1 = int(input("""Please enter a positive integer :"""))
thisNum = isPerfect(num1)
if(thisNum == True):
return num1, is a perfect number
elif(thisNum == False):
return num1 is not a perfect number
def isPerfect(num1):
sumOfDivisors = 0
i = 0
listOfDivisors = getFactors(num1)
for i in range(0, len(listOfDivisors) - 1):
sumOfDivisors = sumOfDivisors + listOfDivisors[i]
i += 1
if(sumOfDivisors/2 == num1):
return True
else:
return False
的原因中,“是不是一个完美的数字” ),它会出来像 (8“是不是一个完美的数字”)
'return'{}不是一个完美的数字'.format(num1)' – 2014-10-30 04:29:35
顺便说一下,你的求和操作是错误的。你从'0 - len(listOfDivisors) - 1'迭代,但是你每次都在你的循环中递增'i',使得每次递增2。考虑使用'sum'函数,比如'sumOfDivisors = sum(getFactors(num1))' – sberry 2014-10-30 04:43:12