Python 检查3个整数是否为斐波那契三元组
我试图输入3个整数,并确定它们是否为斐波那契三元组。斐波那契三元组是三个连续的斐波那契数。有人能帮我找出哪里出了问题吗。谢谢Python 检查3个整数是否为斐波那契三元组,python,fibonacci,Python,Fibonacci,我试图输入3个整数,并确定它们是否为斐波那契三元组。斐波那契三元组是三个连续的斐波那契数。有人能帮我找出哪里出了问题吗。谢谢 def fibs(): a, b = 0, 1 yield a yield b while True: a,b = b, a + b yield b fibInput = (input("please enter 3 numbers separated by commas: ")) n, o, p = [int(i
def fibs():
a, b = 0, 1
yield a
yield b
while True:
a,b = b, a + b
yield b
fibInput = (input("please enter 3 numbers separated by commas: "))
n, o, p = [int(i) for i in fibInput.split(',')]
#print(n,o,p) TEST
for fib in fibs():
if n == fib and o == fib and p == fib:
print("Your numbers are a fibonacci triple.")
break
if fib > n and fib > o and fib > p:
print("your numbers are not a fibonacci triple.")
break
您没有检查这三个数字是否是连续的斐波那契数。您正在检查它们是否都是相同的斐波那契数。循环中的fib具有相同的值 您可以为斐波那契三元组编写其他生成器
def fib_triple():
it = iter(fibs())
a = it.next()
b = it.next()
c = it.next()
while True:
yield(a, b, c)
a, b, c = b, c, it.next()
有什么建议吗?现在我知道我需要更多的变量;只是不确定如何创建这样的函数来完成这项任务,您应该对输入进行排序,以便让我熟悉Python。至于排序,我假设我会使用sort函数,但除此之外我就迷失了。第一个检查是最大的是两个较小数字的和,否则它们肯定不是三元组
def fib_triple():
it = iter(fibs())
a = it.next()
b = it.next()
c = it.next()
while True:
yield(a, b, c)
a, b, c = b, c, it.next()