Three examples of "Python" teach you to write code

Source: Internet
Author: User

This article includes the Fibonacci sequence written in Python, the three-digit number of daffodils and the base code for hundred-dollar chickens:

(i) Fibonacci sequence:

‘‘‘

def Hanshu (n):

n_1 = 1

N_2 = 1

m = n

Sumn = 0

For a in range (1,m+1):

if m = = 1:

Return n_1

If m== 2:

Return n_2

Sumn = n_1 + n_2

N_2 = n_1

n_1 = Sumn

Print (Sumn,end = "")

Hanshu (20)

‘‘‘

Run the results for the above code:

(ii) Find out the number of daffodils within 1-1000:

def Hanshu (n):

Sumn = 0

m = n

For Dix in range (1,4):

m = m% 10

Sumn = Sumn + M * * 3

m = n//10

if Sumn = = N:

Print (N,end = "")

Def hanshu1 ():

For b in Range (1,1000):

Hanshu (b)

HANSHU1 ()

Run the results for the above code:

(three) hundred money to buy hundred chickens: Rooster 5 Yuan A, hen 3 yuan A, chicken 0.5 yuan A, 100 dollars to buy 100 chickens

Q: How many rooster hens and chickens should I buy, a total of several buying methods?

Set: Rooster for x only, hen for y only, chick for z only

x = 0

While x <= 100:

y = 0

While y <= 100:

z = 0

While Z <= 100:

If x + y + z = = + 5 * x +3 * y +0.5 * z = = 100:

Print (x, y, z)

z + = 1

Y + = 1

x + = 1

Run the results for the above code:

This article is original article, reprint please indicate source.

Three examples of "Python" teach you to write code

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.