Perhaps not a very effective solution - using the close formula is more efficient (see Sven answer ), but you can do this:
def fibs(): a,b = 0,1 yield a yield b while True: a,b = b,a+b yield b n = int(raw_input("please, enter a number ")) for fib in fibs(): if n == fib: print "your number is a Fibonacci number!" break if fib > n: print "your number is not a Fibonacci number!" break
The fibs generator gives you a list of Fibonacci numbers. You can view the list, and each number that you can check whether it matches the user entered (in this case you finished), or if it is larger than the one entered by the user (and also made in this case).
Hope this is helpful, at least for understanding Python generators.
source share