阶乘和斐波那契函数有效,但我无法使用第三个函数
Factorial and Fibonacci functions work, but I can't get the third function to work
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 调用其他两个函数来求解斐波那契数或阶乘数。任何 help/explanation 表示赞赏。谢谢!
这是我在 class 中得到的确切提示:
Write a Python program that has 3 Functions:
The first function will be called CallFibOrFac
It will receive a single string as a parameter.
If the string is "Fib" it will call the second function called
Fibonacci
If the string is "Fac" it will call the third function called
Factorial.
The Second function prints the first 10 numbers in the Fibonacci
sequence.
The Third function prints the value of 10 factorial.
We want to make the second and third functions work for cases other
than just the first 10 numbers so add a parameter to the Fibonacci
function and the Factorial function which will tell it how far to go
along the Fibonacci sequence or the Factorial.
试试这个:
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()
你定义 CallFibOrFac
接受输入,但是当你 运行 它在底部时,你不给它输入。要么将函数定义为 def CallFibOrFac()
,要么在 运行 时给它一个字符串输入。您编写脚本的方式是使用 input
请求字符串序列,这意味着您在函数定义中不需要 s。当我去掉定义中的 s 时,脚本 运行s 对我来说很好,但请记住,您需要使用标记回复 'Fib' 或 'Fac' 来声明它们是字符串
此外,如果您发布收到的错误消息,将来会有所帮助
您的代码有一些错误:
- 您的
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)
调用 FibOrFac
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 所指出的)
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 调用其他两个函数来求解斐波那契数或阶乘数。任何 help/explanation 表示赞赏。谢谢!
这是我在 class 中得到的确切提示:
Write a Python program that has 3 Functions:
The first function will be called CallFibOrFac
It will receive a single string as a parameter.
If the string is "Fib" it will call the second function called Fibonacci
If the string is "Fac" it will call the third function called Factorial.
The Second function prints the first 10 numbers in the Fibonacci sequence.
The Third function prints the value of 10 factorial.
We want to make the second and third functions work for cases other than just the first 10 numbers so add a parameter to the Fibonacci function and the Factorial function which will tell it how far to go along the Fibonacci sequence or the Factorial.
试试这个:
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()
你定义 CallFibOrFac
接受输入,但是当你 运行 它在底部时,你不给它输入。要么将函数定义为 def CallFibOrFac()
,要么在 运行 时给它一个字符串输入。您编写脚本的方式是使用 input
请求字符串序列,这意味着您在函数定义中不需要 s。当我去掉定义中的 s 时,脚本 运行s 对我来说很好,但请记住,您需要使用标记回复 'Fib' 或 'Fac' 来声明它们是字符串
此外,如果您发布收到的错误消息,将来会有所帮助
您的代码有一些错误:
- 您的
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)
调用 FibOrFac
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 所指出的)