开发者学堂课程【Python入门 2020年版:挑战练习】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10279
挑战练习
内容简介:
一、九九乘法表
二、“百马百担”问题
三、一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度
一、九九乘法表
1.第一种方法(用 while 写)
j=0
while j<9∶
j+=1
#while 循环需要数据自增,若不自增,则 j<9永远满足,for 循环则不需要,for 生成的是一个有序的序列,每写一个 for 循环就找一次数据
i=0
while i<j∶
i+=1
print( i,‘*’, j ,‘=’, i * j, end=‘\t’,sep=‘’)
print()
运行:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=28
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=35 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
Process finished with exit code 0
2.第二种方法(用 for 写, 不需要写 i+=1,比用 while 方法更简单)
for i in range(1,10)∶
for j in range(1,i+1)∶
print(j,‘*’, i ,‘=’, i * j, end=‘\t’,sep=‘’)
print()
运行∶
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=28
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=35 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
Process finished with exit code 0
二、“百马百担”问题∶
一匹大马能驮3担货,一匹中马能驮2担货,两匹小马能驮1担货,如果用一百匹马驮一百担货,问有大、中、小马各几匹?
假设大马有 x 匹,假设中马有 y 匹,小马(100-x-y)
for x in range(0,100∥3+1)∶
for y in range(0,100∥2+1)∶
if 3 * x + 2 * y +(100-x-y)* 0.5==100∶
print(x,y,(100-x-y))
运行∶
2 30 68
5 25 70
8 20 72
11 15 74
14 10 76
17 5 78
20 0 80
Process finished with exit code 0
三、一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
height=0.08/1000
count=0
while True∶
height *=2
count+=1
if height>=8848.13∶
break
print(count)
运行∶27