5-python作业(史山分享)
很难想象这才第五节课我就已经开始写史山了.
Release
- Setup
- 安装包下载的文件默认在桌面上.
提交作业
正常作业:
example3_6.py
1
2
3
4
5
6
7
8
9
10
11# example3_6.py
# coding=utf-8
aInt = 1
sumInt = 0
while aInt <= 200:
if aInt % 2 == 0:
sumInt = sumInt + aInt
aInt = aInt + 1
print('1-200的偶数和:', sumInt)example3_7.py
1
2
3
4
5
6
7
8
9
10
11# example3_7.py
# coding=utf-8
t = 0
while True:
sale = float(input('请输入订单的销售额'))
if sale <= 0:
break
t += sale
print("总销售额为:", t)example3_9.py
1
2
3
4
5
6
7
8
9
10# example3_9.py
# coding=utf-8
num = int(input('请输入一个大于1的自然数:'))
count = num // 2
while count > 0:
if num % count == 0:
print(count, 'is the max factor of', num)
break
count = count - 1**exercise5_2 **
1
2
3
4
5
6
7
8# exercise5_2
# coding=utf-8
print('1-100之间能被7整除但不能同时被5整除的所有整数:')
i = 7
while i <= 100:
if i%7 == 0 and i%5 != 0:
print(i,end='\t')
i += 1exercise5_3.py
1
2
3
4
5
6
7
8
9# exercise5_3.py
# coding=utf-8
number=15535
count=int(number/2+1)
while count>0:
if number % count==0:
print(count,end=' ')
count=count-1example5_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# example5_4.py
# coding=utf-8
score = 0
while score <= 100:
if score >= 85:
grade = '优秀'
elif score >= 70:
grade = '良好'
elif score >= 60:
grade = '及格'
else:
grade = '不及格'
print('分数', score, '等级', grade)
score += 1exercise5_5
1
2
3
4
5
6
7
8
9# exercise5_5
# coding=utf-8
s = 0
n = 1
while s < 1000:
s += n**2
print(f'{n}\t{s}')
n += 1
print('累计和不超过1000的最大项是n='+str(n-2))我的:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109#-- codeing=utf-8 --
#@Time:2024-3-29
#@Author:zzy
#@File:exercise5.py
#@Software:vscode
# 练习
# 3-6
s = 0
for i in range(2,201,2):
s += i
print(f'1-200的偶数和为{s}')
# 3-7
import re
def if_num(num):
i = re.match('.*?\D.*?',num)
try:
i.group()
except:
return 1
# raise Exception('输入的不是纯数字!')
print('输入的不是纯数字!')
return 0
s = 0
num = input('请输入订单的销售额')
if if_num(num):
s += float(num)
while float(num) > 0 and if_num(num):
num = input('请输入下一个订单的销售额')
if if_num(num):
s += float(num)
print(f'商品的总销售额为{s}')
else:
print('输入的不是纯数字')
# 3-9
import re
def if_num1(num):
i = re.match('.*?\D.*?',num)
try:
i.group()
except:
if '.' in num:
print('输入的不是自然数!')
return 0
else:
if int(num) <= 1:
print('输入的数不大于1!')
return 0
else:
return 1
# raise Exception('输入的不是纯数字!')
print('输入的不是纯数字!')
return 0
num = input('请输入一个大于1的自然数:')
if if_num1(num):
num = int(num)
count = int(num / 2+1)
while count >0:
if num % count == 0:
print(f'{count}是{num}除自身外最大的约数')
break
count -= 1
# exercise5_2
print('1-100之间能被7整除但不能同时被5整除的所有整数:')
for i in range(1,101):
if i%7 == 0 and i%5 != 0:
print(i,end='\t')
# exercise5_3 改错 1
number=15535
count=int(number/2+1)
while count>0:
if number % count==0:
print (count, end=' ')
count=count-1
# exercise5_4 改错 2
score = 0
while score <= 100:
if score >= 85:
grade = '优良'
elif score >= 70:
grade = '中等'
elif score >= 60:
grade = '及格'
else:
grade = '不及格'
if score < 100:
print(f'成绩:{score}\t等级:{grade}')
else:
print(f'成绩:{score}等级:{grade}')
score += 1
# exercise5_5
s = 0
n = 1
while s < 1000:
s += n**2
print(f'{n}\t{s}')
n += 1
print(f'累计和不超过1000的最大项是n={n-2}')
屎山优化
判断是否整数
优化前
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import re
def if_num(num):
i = re.match('.*?\D.*?',num)
try:
i.group()
except:
if '.' in num:
print('输入的不是自然数!')
return 0
else:
if int(num) <= 1:
print('输入的数不大于1!')
return 0
else:
return 1
# raise Exception('输入的不是纯数字!')
print('输入的不是纯数字!')
return 0优化后
1
2
3
4
5
6
7
8def if_int(num):
try:
int(num)
print('整数')
return 1
except:
print('不是整数')
return 0