[Python] Three examples show you how to write code.

Source: Internet
Author: User

[Python] Three examples show you how to write code.

This article includes the Fibonacci series written in Python, the three-digit daffodils, and the basic code for buying a chicken for hundreds of dollars:

(1) Fibonacci series:

'''

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)

'''

For the above Code running result:

 

(2) Find out the daffodils within 1:

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 ()

For the above Code running result:

 

(3) buy a chicken for 0.5 RMB: Rooster for 5 yuan, hen for 3 yuan, chicken for 100 yuan, and 100 chicken for yuan

Q: How many rooster hens and chicks should I buy?

Set rooster to x, hen to y, and chicken to z

X = 0

While x <= 100:

Y = 0

While y <= 100:

Z = 0

While z <= 100:

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

Print (x, y, z)

Z + = 1

Y + = 1

X + = 1

For the above Code running result:

 

This article is an original article. For more information, see the source.

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.