【发布时间】:2013-06-12 02:28:06
【问题描述】:
我目前正在编写一个代码,该代码要求用户输入一个整数并计算该整数所具有的除数。我已经完成了代码,但被困在“返回部分”上。 这是我到目前为止所得到的:
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()
我尝试返回“数字”和“计数”,但似乎无法正常工作。 有什么建议么 ? 顺便说一句,我使用的是 Python 3.3.1
【问题讨论】:
-
您在底线对 main 的调用与之前定义的任何函数都不匹配。您的 findDivisors() 函数将进入无限循环,因为您在 while 循环中减少了 reduction 变量;因此对于任何大于 0 的数字,都会发生无限循环。有关其他修复,请参阅@Simon 的第一个答案
标签: python python-3.x return