Python Chapter 3 homework exercises reference answers

Chapter 3 Reference Answers to After-Class Exercises
1. Multiple Choice Questions

  1. Which of the following descriptions is correct is ______. (B)
    A. The continue statement can be used to terminate the current loop
    B. The break statement can be used to terminate the current loop
    C. The if statement cannot be nested inside the loop statement
    D. The loop statement cannot be nested inside inside the if statement

  2. Which of the following descriptions about the branch structure is correct is ______. (B)
    A. The branch structure must have an else clause
    B. if-else forms a double branch structure
    C. The double branch structure has a compact form, which is realized by using the reserved words if and elif
    D. The nesting of branch structures can only be in the else clause in

  3. Among the following options, the one that is not a basic element of the program flow chart is ______. (B)
    A. Start box B. Prompt box C. Judgment box D. Input and output box

  4. Which of the following statements about the algorithm is incorrect is ______. (B)
    A. Algorithms are methods and steps to solve problems B. Algorithms must have inputs
    C. Algorithms can stop after a finite number of steps D. Algorithms must have outputs

  5. The following that is not part of the IPO model are ______. (A)
    A. InOut B. Process C. Input D. Output

  6. The output of the following program is ______. (B)
    a,b=5,0
    if a>5:
    b=b+2
    b=b+2
    print(b)
    A. 0 B. 2 C. 7 D. 9

  7. The output of the following program is ______. (B)
    x=3
    if x>=3:
    x=x+1
    elif x>=4:
    x=x+2
    elif x>5:
    x=x+3
    print(x)
    A. 3 B. 4 C. 6 D. 9

  8. The output of the following program is _____. ©
    x=7
    y=12 if x>5 else 20
    print(y)
    A. 7 B. 5 C. 12 D. 20

  9. The output of the following program is ______. (B)
    i=1
    s=0
    while i<=10:
    if i%3!=0:
    s=s+i
    i=i+1
    print(s)
    A. 18 B. 37 C. 55 D. 0

  10. The output of the following program is ______. (B)
    i=0
    while i<10:
    i=i+2
    if i>6:
    break
    print(i,end=",")
    else:
    print(12)
    print(123)
    A. 0,2,4, 6,8,123 B. 2,4,6,123 C. 0,2,4,6,8 12123 D. 2,4,6,12

2. Fill in the blanks
11. Commonly used methods for expressing algorithms include: natural language, (traditional flow chart), (pseudo-code), (NS flow chart (box diagram). 12. Python selection
structure includes single-branch selection structure, (double branch selection structure), (multi-branch selection structure) and nested selection structure.
13. Python loop structure includes (for loop) and (while loop) two loop structures.
14. Python loop structure can use three special statements as needed : (continue statement) statement, (break statement) statement and else statement.
15. In the loop statement, the function of the (break statement) statement is to exit the current loop structure in advance.
16. In the loop statement, the (continue statement) statement The function is to advance to the next loop.
7. The value of the expression 20 if 10 > 5 else -20 is (20).
17. The value of the expression 5 if 10 > 20 else ( 0 if 5 > 8 else 1) is ( 1).
18. for n in range(5): print( n, end = ',') the running result (0,1,2,3,4,).
19. for i in range(1 , 20 , 2): The result of print(i, end = ';') (1;3;5;7;9;11;13;15;17;19;).

3. Program design question
20. Program to calculate the following piecewise function values: .

insert image description here

import math
x=int(input("x:"))
if x<0:
    y=2*math.pow(x,4)-3*math.pow(x,3)
else:
    if x==0:
        y=math.e**2
    else:
        y=3*math.sqrt(2*x)
print("y={}".format(y))
  1. Write a program to realize the following function: seek the smallest n that satisfies 1+2+3+4+┄+n>2020 and output it (it is required to use a loop to realize).
n = 0 
z = 0 
while(z<=2020):  
        n+=1  
        z+=n 
print("n={0}".format(n))
  1. Given a positive integer less than 1000, the program finds the number of digits, and prints out the numbers on each digit in reverse order. For example: the original number is 456, then the output is 654.

x=int(input('请输入一个数:\n'))
while x<1000:
a=x//100
b=x%100//10
c=x%10
break
if a!= 0:
print('3位数:', c,b,a)
elif b!= 0:
print('2位数:',c,b)
elif c!= 0:
print('1位数:', c)
  1. Input a string, program statistics and output the number of uppercase English letters and numbers in it.
print("Input a string: ")
str1 = input() 
no_of_ capital_letters, no_of_digits = 0,0 
for c in str1:
    if (c>='A' and c<='Z'):
        no_of_capital_letters += 1
    if c>='0' and c<='9':
        no_of_digits += 1 
print("Input string is: ", str1)
print("Total number of capital_letters: ", no_of_capital_letters)
print("Total number of digits: ", no_of_digits)
  1. Program to count the number of all prime numbers directly from 100 to 1000, and print out their sum.
num=0
sum=0
for n in range(100,1000):
   for x in range(2, n):
      if (n%x==0):
         break
      else:
         num+=1
sum+=n
print(“num={
    
    0},sum={
    
    0}.format(num,sum))
  1. Count and output the number of all numbers between 500 and 2020 that are divisible by 7 and have a unit digit of 2 (required to be implemented with a loop).
n = 0 
for i in range(500,2021): 
       if (i%7==0 and i%10==2): 
           n+=1 
print("n={0}".format(n))

Guess you like

Origin blog.csdn.net/weixin_50804299/article/details/128466688