Python3 Fibonacci sequence (Fibonacci sequence)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.