题目一:最小公倍数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#最小公倍数 def lcm(x, y): if x > y: greater = x else: greater = y for i in range(1,greater + 1): if((x % i == 0) and (y % i == 0)): lcm = i while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 return lcm num1 = int(input("输入第一个数字: ")) num2 = int(input("输入第二个数字: ")) print( num1,"和", num2,"的最小公倍数为", lcm(num1, num2))
题目二:最大公约数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13def hcf(x, y): if x > y: smaller = y else: smaller = x for i in range(1,smaller + 1): if((x % i == 0) and (y % i == 0)): hcf = i return hcf num1 = int(input("输入第一个数字: ")) num2 = int(input("输入第二个数字: ")) print( num1,"和", num2,"的最大公约数为", hcf(num1, num2))
题目三:bubbleSort冒泡排序
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#bubbleSort冒泡排序 def bubbleSort(list1): n = len(list1) for i in range(n): for j in range(0,n-i-1,1): if list1[j] > list1[j+1]: temp = list1[j] list1[j] = list1[j+1] list1[j+1] = temp list1 =[12,125,124,124.5,124.6,123/123,95%9] bubbleSort(list1) print ("排序后的数组:") print(list1) # 打印结果 # [1.0, 5, 12, 124, 124.5, 124.6, 125] for i in range(len(list1)): print ("%s" %list1[i]) #打印结果(小数) # 1.0 # 5 # 12 # 124 # 124.5 # 124.6 # 125 for i in range(len(list1)): print ("%d" %list1[i]) #打印结果(整数) # 1 # 5 # 12 # 124 # 124 # 124 # 125
题目四: BMI指数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13height = 1.53 weight = 40 bmi = weight / (height ** 2) if bmi < 18.5: print('过轻') elif 18.5 <= bmi < 25: print('正常') elif 25 <= bmi < 28: print('过重') elif 28 <= bmi < 32: print('肥胖') else : print('严重肥胖')
题目五:
用1、2、3、4组成互不相同无重复数字的三位数,都是多少,一共几个
复制代码
1
2
3
4
5
6
7
8
9
10
11# 用1、2、3、4组成互不相同无重复数字的三位数,都是多少,一共几个 a = 0 for i in range(1,5,1): for j in range(1,5,1): for k in range(1,5,1): if i != j and i != k and j != k: print(i*100+j*10+k) a = a + 1 print(a)
题目六: 石头剪子布小游戏
复制代码
1
2
3
4
5
6
7
8
9
10
11
12#石头剪子布小游戏 import random a = random.randint(1,3) b = random.randint(1,3) #1 = 石头 2 = 剪刀 3 = 布 if (a == 1 and b == 2) or (a == 2 and b == 3) or (a == 3 and b == 1): print('你赢了') elif a == b: print('平局了') else : print('你输了')
题目七:阶乘
复制代码
1
2
3
4
5
6
7
8
9
10
11
12#阶乘 import random a = random.randint(-25,25) b = 1 if a < 0: print('对不起,负数没有阶乘') elif a == 0: print('0的阶乘为1') else: for i in range(1,a + 1): b = b*i print("%d 的阶乘为 %d" %(a,b))
题目八:用TURTLE库绘制圆圈并组成倒三角形
如下图所示:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49import turtle as t t.speed(20) for i in range(5): t.pendown() t.circle(50) t.penup() t.fd(100) t.right(180) t.fd(150) t.pendown() for i in range(4): t.circle(50) t.penup() t.fd(100) t.pendown() t.penup() t.left(90) t.fd(100) t.left(90) t.fd(150) for i in range(3): t.pendown() t.left(180) t.circle(50) t.left(180) t.penup() t.fd(100) t.pendown() t.penup() t.right(90) t.fd(100) t.right(90) t.fd(150) t.pendown() t.circle(50) t.penup() t.fd(100) t.pendown() t.circle(50) t.penup() t.fd(60) t.left(90) t.fd(140) t.left(90) t.fd(160) t.left(90) t.pendown() t.circle(50) t.mainloop()
题目八:用TURTLE库绘制圆圈并组成正三角形
如下图所示:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56import turtle as t t.speed(20) t.penup() t.goto(0,100) t.pendown() for i in range(1): t.circle(50) t.penup() t.fd(100) t.pendown() t.left(90) t.penup() t.right(180) t.right(90) t.fd(50) for i in range(2): t.pendown() t.circle(50) t.penup() t.fd(100) t.pendown() t.penup() t.left(90) t.fd(200) t.left(90) t.fd(50) t.pendown() for i in range(3): t.circle(50) t.penup() t.fd(100) t.pendown() t.left(90) t.penup() t.right(180) t.right(90) t.fd(50) t.pendown() for i in range(4): t.circle(50) t.penup() t.fd(100) t.pendown() t.penup() t.left(90) t.fd(200) t.left(90) t.fd(50) t.pendown() for i in range(5): t.pendown() t.circle(50) t.penup() t.fd(100) t.hideturtle() t.mainloop()
最后
以上就是忧心墨镜最近收集整理的关于python练习题(1)的全部内容,更多相关python练习题(1)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复