Notes on computer level 2 Python brushing questions ------ basic operation questions 5, 6, 15 (check random)


insert image description here

fifth question

Topic:
insert image description here
Analysis:

  • import random, import random library.
  • name = random.sample(brandlist,1), Randomly find an element from the brandlist list, and then assign it to name. sample(pop,k) means to randomly select k elements from the pop type, and return it as a list type.
  • Note: random.seek(0), set the random number seed, the purpose is to keep the output of the result unchanged, which is convenient for comparison with the standard answer and convenient for scoring.

Answer:

# 请在...处使用一行或多行代码替换
# 请在______处使用一行代码替换
# 注意:请不要修改其他已给出代码

import random
brandlist = ['华为','苹果','诺基亚','OPPO','小米']
random.seed(0)
name = random.sample(brandlist,1)
print(name)

operation result:
insert image description here

Question 12

Topic:
insert image description here
insert image description here
Analysis:

  • Use 123 as the random seed, so the first empty:random.seed(123)
  • Randomly generate 10 random numbers and give a for loop, so it loops 10 times. Output one at a time, so the second empty range(10)。
  • The third empty outputs a randomly generated random number between 1 and 999, using the randint() function, randint(a,b) generates an integer between [a,b]. **random.randint(1,999)** is required by the meaning of the question.

Answer:

import random
random.seed(123)
for i in range(10):
    print(random.randint(1,999), end=",")

operation result:
insert image description here

Question 15

topic:
insert image description here

insert image description here
Parse:

  • With 0 as the random seed, so the first is empty:random.seed(0)
  • The second empty outputs a randomly generated random number between 1 and 97, using the randint() function, randint(a,b) generates an integer between [a,b]. random.randint(1,97) is what the title requires.
  • Calculate the sum of the squares of 5 random numbers and accumulate after squares, so the third empty s =s+pow(n,2), pow(n,2) means the square of n.

Answer:

import random
random.seed(0)
s = 0
for i in range(5):
    n = random.randint(1,97)  # 产生随机数
    s = s + pow(n,2) 
print(s)

operation result:
insert image description here

Question 18

topic:
insert image description here
insert image description here

Parse:

  • With 100 as the random seed, so the first empty:random.seed(100)
  • Output a randomly generated random number between 1 and 9, and calculate the cubic sum of 3 random numbers, accumulate after the cube, so the second empty
    for i in range(3):
      n = random.randint(1,9) # generate random number
      s = s + pow(n,3)

    Or write the n part directly:
    for i in range(3):
      s = s + pow( random.randint(1,9) ,3)

Answer:

import random
random.seed(100)
s = 0
for i in range(3):
    n = random.randint(1,9)  # 产生随机数
    s = s + pow(n,3) 
print(s)

operation result:

Question 9

Topic:
Analysis:
Answer:
Running result:

Guess you like

Origin blog.csdn.net/weixin_47296493/article/details/130310557