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