Python Fundamentals 3 (Chapter 2 Choice)

1. Multiple choice questions

2. Multiple Choice

 3. Programming examples

Write a function to print an asterisk triangle, use this function, pass in data 2, 3, and 4 to print the following Christmas tree graphics Be Be

   *
  ***
   *
  ***
 *****
   *
  ***
 *****
*******
def Print(x,y,z):
    i = 1
    m = x-1
    while i <= x:
        print(" "*(z-x+m-1),"*"*(2*i-1))
        i = i+1
        m = m-1
    i = 1
    m = y-1
    while i <= y:
        print(" "*(z-y+m-1),"*"*(2*i-1))
        i = i+1
        m = m-1
    m=z-1
    for i in range(z):
        print(" "*m,end="")
        m = m-1
        print("*"*(2*(i+1)-1))


# x,y,z=map(int,input().split('、'))
Print(2,3,4)

 

A hundred dollars to buy a hundred chickens

Use 100 yuan to buy 100 chickens; Be Be

Among them, a rooster costs 5 yuan, a hen costs 3 yuan, and a chick costs 3 yuan. Be Be

How many ways to buy the output Be

‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

Please use a double loop to complete this problem, each type of chicken can be 0 Be Be

Example of input and output

enter output
Example 1 none

Cock Hen Chick
0 25 75
4 18 78
8 11 81
12 4 84

count=0
print('公鸡 母鸡 小鸡')
for x in range(0,21):
    for y in range(0,34):
        for z in range(3,100):
            if x*5+y*3+z/3==100 and x+y+z==100:
                count+=1
                print('%d %d %d'%(x,y,z))

 

Monte Carlo method to find the value of pi

Monte Carlo method is suitable for finding the area of ​​a graph according to probability Be Be

Find an unknown amount of data based on the area of ​​two graphs Be Be

Please randomly generate 10,000 points to determine whether they fall into a 1/4 circle with a radius of 1. Be Be

The ratio of the probability of passing to the area of ​​a square with side length 1 Be Be

Calculate the value of π, requiring the output to be forced to two decimal places Be Be

‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

Example of input and output

enter output
Example 1 none 3.14
import random
import math
random.seed(180)
hit = 0.0    #表示随机点在圆内的次数
#请将代码补充完整
darts=10000
for i in range(1,darts+1):
    x,y=random.random(),random.random()
    dist=math.sqrt(x**2+y**2)
    if dist<=1.0:
        hit=hit+1
pi = 4.0 * (hit/darts)
print('{:.2f}'.format(pi))

 

Guess you like

Origin blog.csdn.net/qq_54587141/article/details/124123892