Python programming classic case [examination questions] find all numbers divisible by 3 and divisible by 5 in a certain range, and the sum of these numbers

This article explores classic cases of Python programming with you, allowing you to learn Python immersively. You can think about the topic first, and then compare it with the problem-solving methods in this article. If you have different opinions, welcome to discuss with me in the official account.

  

  

1. Classic case [examination question]

  
question:

1000中能被3整除且能被5整除的所有正整数及这些数的和

enter:

1000

output:

1000中能被3整除且能被5整除的正整数有 15
1000中能被3整除且能被5整除的正整数有 30
1000中能被3整除且能被5整除的正整数有 45
1000中能被3整除且能被5整除的正整数有 60
1000中能被3整除且能被5整除的正整数有 75
1000中能被3整除且能被5整除的正整数有 90
1000中能被3整除且能被5整除的正整数有 105
1000中能被3整除且能被5整除的正整数有 120
1000中能被3整除且能被5整除的正整数有 135
1000中能被3整除且能被5整除的正整数有 150
1000中能被3整除且能被5整除的正整数有 165
1000中能被3整除且能被5整除的正整数有 180
1000中能被3整除且能被5整除的正整数有 195
1000中能被3整除且能被5整除的正整数有 210
1000中能被3整除且能被5整除的正整数有 225
1000中能被3整除且能被5整除的正整数有 240
1000中能被3整除且能被5整除的正整数有 255
1000中能被3整除且能被5整除的正整数有 270
1000中能被3整除且能被5整除的正整数有 285
1000中能被3整除且能被5整除的正整数有 300
1000中能被3整除且能被5整除的正整数有 315
1000中能被3整除且能被5整除的正整数有 330
1000中能被3整除且能被5整除的正整数有 345
1000中能被3整除且能被5整除的正整数有 360
1000中能被3整除且能被5整除的正整数有 375
1000中能被3整除且能被5整除的正整数有 390
1000中能被3整除且能被5整除的正整数有 405
1000中能被3整除且能被5整除的正整数有 420
1000中能被3整除且能被5整除的正整数有 435
1000中能被3整除且能被5整除的正整数有 450
1000中能被3整除且能被5整除的正整数有 465
1000中能被3整除且能被5整除的正整数有 480
1000中能被3整除且能被5整除的正整数有 495
1000中能被3整除且能被5整除的正整数有 510
1000中能被3整除且能被5整除的正整数有 525
1000中能被3整除且能被5整除的正整数有 540
1000中能被3整除且能被5整除的正整数有 555
1000中能被3整除且能被5整除的正整数有 570
1000中能被3整除且能被5整除的正整数有 585
1000中能被3整除且能被5整除的正整数有 600
1000中能被3整除且能被5整除的正整数有 615
1000中能被3整除且能被5整除的正整数有 630
1000中能被3整除且能被5整除的正整数有 645
1000中能被3整除且能被5整除的正整数有 660
1000中能被3整除且能被5整除的正整数有 675
1000中能被3整除且能被5整除的正整数有 690
1000中能被3整除且能被5整除的正整数有 705
1000中能被3整除且能被5整除的正整数有 720
1000中能被3整除且能被5整除的正整数有 735
1000中能被3整除且能被5整除的正整数有 750
1000中能被3整除且能被5整除的正整数有 765
1000中能被3整除且能被5整除的正整数有 780
1000中能被3整除且能被5整除的正整数有 795
1000中能被3整除且能被5整除的正整数有 810
1000中能被3整除且能被5整除的正整数有 825
1000中能被3整除且能被5整除的正整数有 840
1000中能被3整除且能被5整除的正整数有 855
1000中能被3整除且能被5整除的正整数有 870
1000中能被3整除且能被5整除的正整数有 885
1000中能被3整除且能被5整除的正整数有 900
1000中能被3整除且能被5整除的正整数有 915
1000中能被3整除且能被5整除的正整数有 930
1000中能被3整除且能被5整除的正整数有 945
1000中能被3整除且能被5整除的正整数有 960
1000中能被3整除且能被5整除的正整数有 975
1000中能被3整除且能被5整除的正整数有 990
1000中能被3整除且能被5整除的所有正整数的和= 33165

  
  

2. Classic case problem-solving method

  

1 Method 1: Use the for loop to find the numbers that meet the conditions and sum them up

  
The first way to solve the problem in the above case is:
  
step1: Use the for loop to list all the numbers in the interval.
  
Step2: Use the if statement to judge whether the number is divisible by both 3 and 5, and if so, print out the number and count it into the summation parameter.
  
The specific code is as follows:

num = int(input('请输入区间的最大值'))
sum_num = 0
for i in range(1, num+1):
    if i % 3==0 and i % 5 == 0:
        print('1000中能被3整除且能被5整除的正整数有',i)
        sum_num += i
print('1000中能被3整除且能被5整除的所有正整数的和=', sum_num)   
input('请输入范围的最大值'):手动输入待求区间的最大值。

i % 3==0:判读i是否整除3,若能则返回True,否则返回False

If the maximum value of the manual input interval is 100, the result is:

请输入范围的最大值100
1000中能被3整除且能被5整除的正整数有 15
1000中能被3整除且能被5整除的正整数有 30
1000中能被3整除且能被5整除的正整数有 45
1000中能被3整除且能被5整除的正整数有 60
1000中能被3整除且能被5整除的正整数有 75
1000中能被3整除且能被5整除的正整数有 90
1000中能被3整除且能被5整除的所有正整数的和= 315

You can manually verify that the result is correct. The advantage of this method is that the logic is simple and clearer to understand.

  
  

2 Method 2: Use the for loop to find the number that satisfies the condition, store it in the list and sum it

  
The second way to solve the problem in the above case is:
  
step1: Use the for loop to list all the numbers in the interval.
  
Step2: Use the if statement to judge whether the number is divisible by both 3 and 5, and if so, add the number to the list.
  
step3: Print the list and list sum results.
  
The specific code is as follows:

num = int(input('请输入区间的最大值'))
num_list = []
for i in range(1, num+1):
    if i % 3==0 and i % 5 == 0:
        #print('1000中能被3整除且能被5整除的正整数有',i)
        num_list.append(i)
print('1000中能被3整除且能被5整除的正整数列表如下:')
print(num_list)
print('1000中能被3整除且能被5整除的所有正整数的和=', sum(num_list))

If the manual date is 1000, the result is:

请输入范围的最大值1000
1000中能被3整除且能被5整除的正整数列表如下:
[15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, 900, 915, 930, 945, 960, 975, 990]
1000中能被3整除且能被5整除的所有正整数的和= 33165

You can manually verify that the result is correct, and interested friends can also copy the code into Python and enter other values ​​for verification. So far, the classic case of programming in Python [examination question] seeks all numbers that are divisible by 3 and 5 within a certain range, and the sum of these numbers has been explained.

  
If you want to know more about functions in Python, you can read related articles on the "Learning Python" module in the official account.
  
You may be interested in:
Draw Pikachu with Python Draw a
word cloud map
with Python Draw 520 eternal heartbeats with
Python With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Guess you like

Origin blog.csdn.net/qq_32532663/article/details/127950396