我正在尝试输入3个整数并确定它们是否是Fibonacci Triples。 Fibonacci Triple是三个连续的Fibonacci数。有人可以帮我弄清楚我哪里出错了。谢谢!
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
答案 0 :(得分:0)
if n == fib and o == fib and p == fib:
您没有检查这三个数字是否是连续的斐波纳契数。您正在检查他们是否所有相同的 Fibonacci号码。
答案 1 :(得分:0)
在你的循环中,fib
具有相同的值。
您可以为Fibonacci三元组编写其他生成器
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()