Python 函数返回问题
我目前正在编写一个代码,要求用户输入一个整数,并计算整数的除数。我已经完成了代码,但仍停留在“返回部分”上。 到目前为止,我得到的是:Python 函数返回问题,python,python-3.x,return,Python,Python 3.x,Return,我目前正在编写一个代码,要求用户输入一个整数,并计算整数的除数。我已经完成了代码,但仍停留在“返回部分”上。 到目前为止,我得到的是: def findDivisors(number): decrease = number count = 0 while ( decrease >= 1): if ( number%decrease == 0 ): count=count+1 decrease=decrease-1 d
def findDivisors(number):
decrease = number
count = 0
while ( decrease >= 1):
if ( number%decrease == 0 ):
count=count+1
decrease=decrease-1
def main(count):
number = int(input("Please enter a positive integer : "))
print(count)
main()
我试着同时返回“number”和“count”,但似乎无法实现。
有什么建议吗?
顺便说一句,我使用的是Python 3.3.1
findDivisors()
的末尾需要返回计数
findDivisors()
中的缩进似乎不正确(reduce=reduce-1
应该缩进到if
语句的第一行count
作为main()
函数的参数,特别是当您在没有任何参数的情况下调用main()
时findDivisors()
。我建议将print
调用替换为print(findDivisors(number))
太好了!谢谢您的详细回答。您对main的调用与之前定义的任何函数都不匹配。您的findDivisors()函数将进入无限循环,因为您没有在while循环中递减reduce变量;因此,对于任何大于0的数字,都将出现无限循环。有关其他修复方法,请参阅@Simon的第一个答案