Outputs the number of previous Fibonacci sequence numbers.
Using functions
#!/usr/bin/env python#-*-coding:utf-8-*-# author:hiuhung wan#----Fibonacci sequence (Fibonacci sequence)-----def check_num ( NUMBER:STR): "" check the input string, positive integer, return ture, otherwise return false :p Aram Number: Input string : return: Meet the requirements, returns ture, Non-conforming return false ' ' # input cannot be 1, to be greater than or equal to 2 if Intput.isdigit () and intput! = ' 1 ': return True else: Return falsedef fib (times:int): " find a list of Fibonacci numbers:p Aram times: Number of Fibonacci sequences : return: Returns a list of Fibonacci sequences " # first two numbers are defined fib = [1, 1] for I in Range (count-2): fib.append (fib[-1] + fib[-2]) return fibif __name__ = = ' __main__ ': intput = input ("How many Fibonacci sequences do you want to output? ") if Check_num (intput): count = Int (intput) print (FIB (count)) else: print (" Please enter a positive integer greater than 1 " )
Effect:
C:\Python36\python.exe d:/py/1704/day02/fib_test.py How many Fibonacci numbers do you want to output? 20[1, 1, 2, 3, 5, 8, 610, 144, 233, 377, 987, 1597, 2584 4181 with exit code 0
Python3 Fibonacci sequence (Fibonacci sequence)