代码之家  ›  专栏  ›  技术社区  ›  whitericenoodle

阶乘和斐波那契函数可以工作,但我无法让第三个函数工作

  •  -1
  • whitericenoodle  · 技术社区  · 6 年前
     def Factorial(n):
        n=int(input("Input a number?"))
        if n==0:
            return 1
        else:
            return n * Factorial(n-1)    
    def Fibonacci(num):
        i=0
        present=1
        previous=0
        while i<=num:
                nextterm=present+previous
                present=previous
                previous=nextterm
                i=i+1
                print("The fibonacci number for", i, "is", nextterm)
    def CallFibOrFac(s):
        s=input('Fib or Fac?')
        if s == 'Fib':
            num=input('Up to what number?')
            print(Fibonacci(num))
        elif s == 'Fac':
            n=input('What number?')
            print(Factorial(n))   
    CallFibOrFac()
    

    我花了很多时间来解决这个任务。我不明白我应该如何让我定义的函数CallFibOrFac调用其他两个函数来解斐波那契数或阶乘数。感谢您的帮助/解释。谢谢

    这正是我在课堂上留下的提示:

    编写一个具有3个函数的Python程序:

    第一个函数将被称为CallFibOrFac

    它将接收单个字符串作为参数。

    如果字符串为“Fib”,它将调用调用的第二个函数 斐波那契

    如果字符串为“Fac”,它将调用调用的第三个函数 阶乘。

    第二个函数打印斐波那契曲线中的前10个数字 序列

    第三个函数打印10阶乘的值。

    我们想让第二和第三个功能适用于其他情况 而不仅仅是前10个数字,所以在斐波那契曲线中添加一个参数 函数和阶乘函数,它们将告诉它要走多远 沿着斐波那契序列或阶乘。

    3 回复  |  直到 6 年前
        1
  •  3
  •   Adriano Martins    6 年前

    您的代码中有一些错误:

    • 你的 CallFibOrFac 函数接收字符串,但将其覆盖。任务说它将只传递字符串,按给定的方式使用它;
    • 您在阶乘方法的每次迭代中都需要一个新的输入,它应该在函数开始之前给出;
    • 在将字符串传递给斐波那契函数之前,您忘记了将其转换为int。

    也就是说,所有校正的功能应为:

    阶乘函数:

    def Factorial(n):
        # Removed the input read
        if n==0:
            return 1
        else:
            return n * Factorial(n-1)
    

    斐波那契函数:

    # It is working as expected
    def Fibonacci(num):
        i=0
        present=1
        previous=0
        while i<=num:
            nextterm=present+previous
            present=previous
            previous=nextterm
            i=i+1
            print("The fibonacci number for", i, "is", nextterm)
    

    CallFibOrFac公司

    def CallFibOrFac(s):
        # Moved the number detection out of the ifs, since both will use it
        # Note that the task says it will _receive_ the string as parameter, so you don't need to ask here.
        num=int(input('Up to what number?'))
        if s == 'Fib':
            print(Fibonacci(num))
        elif s == 'Fac':
            print(Factorial(num))
    

    注意:还有一些东西需要修复和/或改进,我刚刚研究过,这可能是您现在面临的一个问题(正如@abarnert所指出的)

        2
  •  -1
  •   Novice    6 年前

    您定义 CallFibOrFac 接受输入,但当您在底部运行它时,您不会给它一个输入。将函数定义为 def CallFibOrFac() ,或在运行时为其提供字符串输入。您编写脚本的方式,通过询问字符串序列 input ,表示在函数的定义中不需要s。当我去掉定义中的s时,脚本对我来说运行得很好,但请记住,您需要用带有标记的“Fib”或“Fac”进行回复,以声明它们是字符串

    此外,如果您在将来发布收到的错误消息,这将很有帮助

        3
  •  -2
  •   Rafał Ludwiczak    6 年前

    尝试以下操作:

    def Factorial(n):
        n=int(input("Input a number?"))
        if n==0:
            return 1
        else:
            return n * Factorial(n-1)
    
    def Fibonacci(num):
        i=0
        present=1
        previous=0
        while i<=num:
            nextterm=present+previous
            present=previous
            previous=nextterm
            i=i+1
            print("The fibonacci number for", i, "is", nextterm)
    
    def CallFibOrFac():
        s=input('Fib or Fac?')
        if s == 'Fib':
            num=input('Up to what number?')
            print(Fibonacci(int(num)))
        elif s == 'Fac':
            n=input('What number?')
            print(Factorial(int(n)))
    
    CallFibOrFac()