Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数返回问题_Python_Python 3.x_Return - Fatal编程技术网

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的第一个答案