Python学习(是我的课堂上学到的东西)不包含课外

2023-09-19课堂

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
"""
工资系统
"""
import random
# 公司账户
account = float(input("公司账户多少钱:"))
# 基本工资
base_account = float(input("基本工资多少:"))
# 浮动
var_base_account = float(input("浮动工资多少:"))
# 这是循环一个id
#计数器
count = 0
sid = 0
for id in range(10,20):
# 这里是工作的分数,当然使用是随机生成,可以改成输入也行
work_score = random.randint(60,100)
# 浮动工资的计算
var_base_salary = var_base_account * work_score / 100
# 这个月的工资计算
salary = base_account + var_base_salary
# 判断
if account - salary >= 0:
print(f"工号是{id},绩效分{work_score},基本工资{base_account},浮动工资{var_base_salary},总工资{salary}")
print("已发工资")
account -= salary
count += 1
continue
else:
print(f"工号:{id},未发.原因余额不足")
sid += 1
continue
print(f"余额{account}")
print(f"剩余没发的人数:{sid}")
ssaccount = account / sid
print(f"每人平分{ssaccount}")
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
"""
这是对0919的学习
"""
print(f"warning:开始时间必须小于结束时间")
sday = int(input("请输入开始的时间:"))
eday = int(input("请输入结束的时间:"))
# 初步的判断是否执行
if sday > eday:
print(f"输入有误")
elif sday == 'q' and eday == 'q':
print("程序退出")
elif sday < 0 or eday >7:
print("天数不合理")
else:
count = 0
sumcount = 0
while sday <= eday:
print(f"\n周{sday}计划跑")
count += 1
sum = int(input(f"输入{sday}天要跑的公里数:"))
sday += 1
if sum <= 0:
print("\t摆烂吧!")
break
print("\t",end='')
dis = 0
print(f"路径图:", end='')
while dis < sum:
dis += 1
print(f"{dis}km", end='')
if dis < sum:
print(f"》", end='')
print(f"\n\t合计{dis}km", end='')
sumcount += dis
print('\n')
print(f"{count}day结束")
print(f"这几天跑了{sumcount}km")
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
"""
这是求1到10之间所有的质数
"""
import random

for m in range(1,10):
if m <= 1:
continue
for n in range(2,m):
if m % n == 0:
break
else:print(f"{m} ", end='')
print(f"\n程序结束")
# 这是第二种方式
# 通过试除法去实现
# 如果是一个数分解为若干素数的积,如果没有素因子,那么它本身就是一个素数。
# 利用开方是中间的数,一半如果找不到满足的数,再大也无意义了
import math
def zhishu(number):
if number <= 1:
return False
for n in range(2,int(math.sqrt(number)+1)):
if number % n == 0:
return False
else: return True
pri = []
for m in range(1,100):
if zhishu(m):
pri.append(m)
print(pri)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
新的语法(语法糖)以及程序的调试
"""
num = 1
while num <= 10:
print(f"{num} ",end='')
num += 1
else:
print("谢谢使用~")

for num in range(1,11):
print(f"{num} ",end='')
else:
print("谢谢使用~")
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
"""
查询字符串中,有多少个字符,其他功能见打印的
"""
# 这个地方可以输入中英文的句子,自动类型转换
word = str(input("请输入一句句子:"))
# 计数器
# 这个是统计空格
count = 0
# 这个计数器是统计逗号的
count1 = 0
# 这个计数器是统计句号的
count2 = 0
# 这个计数器是统计的字符串里有多少个字符
count_word = 0
# 遍历整个字符串
for strd in word:
# 这个地方无论是什么字符,都进行统计
count_word +=1
# 统计空格
if strd == ' ':
count += 1
# 统计中英文逗号
if strd == ',' and strd == ',':
count1 += 1
# 统计中英文句号
if strd == '.' and strd == '。':
count2 += 1
# 输出计数器结果
print(f"合计有{count_word}个字符,其中有{count}个空格,有{count1}个逗号,有{count2}个句号")
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
特殊数学数求法
"""
import random
import time
# 水仙花数个数
pri = []
count = 0
# 玫瑰花数个数
pri1 = []
count1 = 0
# 五角星数个数
pri2 = []
count2 = 0
# 六合数个数
pri3 = []
count3 = 0
# 北斗七星数个数
pri4 = []
count4 = 0
# 八仙数个数
pri5 = []
count5 =0
# 九九重阳数个数
pri6 = []
count6 = 0
# 十全十美数个数
pri7 = []
count7 = 0
# 水仙花数
# 穷举法
stime = time.time()
for i in range(100,1000):
# / 是取个float值,// 是取int整形
a = i // 100
b = i // 10 % 10
c = i % 10
if i == a ** 3 + b ** 3 + c ** 3 :
count += 1
pri.append(i)
print(f"有{count}个水仙花数")
print(pri, end=' ')
print("是水仙花数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 玫瑰花数
# 穷举法
stime = time.time()
for j in range(1000,10000):
a = j // 1000
b = j // 100 % 10
c = j // 10 % 10
d = j % 10
if j == a ** 4 + b ** 4 + c ** 4 +d ** 4:
count1 += 1
pri1.append(j)
print(f"有{count1}个玫瑰花数")
print(pri1, end=' ')
print("是玫瑰花数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 五角星数
# 穷举法
stime = time.time()
for x in range(10000,100000):
a = x // 10000
b = x // 1000 % 10
c = x // 100 % 10
d = x // 10 % 10
e = x % 10
if x == a ** 5 + b ** 5 + c ** 5 + d ** 5 + e ** 5:
count2 += 1
pri2.append(x)
print(f"有{count2}个五角星数")
print(pri2, end=' ')
print("是五角星数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 六合数
# 穷举法
stime = time.time()
for y in range(100000,1000000):
a = y // 100000
b = y // 10000 % 10
c = y // 1000 % 10
d = y // 100 % 10
e = y // 10 % 10
f = y % 10
if y == a ** 6 + b ** 6 + c ** 6 + d ** 6 + e ** 6 + f ** 6:
count3 += 1
pri3.append(y)
print(f"有{count3}个六合数")
print(pri3, end=' ')
print("是六合数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 北斗七星数
# 穷举法
stime = time.time()
for z in range(1000000,10000000):
a = z // 1000000
b = z // 100000 % 10
c = z // 10000 % 10
d = z // 1000 % 10
e = z // 100 % 10
f = z // 10 % 10
g = z % 10
if z == a ** 7 + b ** 7 + c ** 7 + d ** 7 + e ** 7 + f ** 7 + g ** 7:
count4 +=1
pri4.append(z)
print(f"有{count4}个北斗七星数")
print(pri4, end=' ')
print("是北斗七星数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 八仙数
# 穷举法
stime = time.time()
for a1 in range(10000000,100000000):
a = a1 // 10000000
b = a1 // 1000000 % 10
c = a1 // 100000 % 10
d = a1 // 10000 % 10
e = a1 // 1000 % 10
f = a1 // 100 % 10
g = a1 // 10 % 10
h = a1 % 10
if a1 == a ** 8 + b ** 8 + c ** 8 + d ** 8 + e ** 8 + f ** 8 + g ** 8 + h ** 8:
count5 +=1
pri5.append(a1)
print(f"有{count5}个八仙数")
print(pri5, end=' ')
print("是八仙数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 九九重阳数
# 穷举法
stime = time.time()
for a2 in range(100000000,1000000000):
a = a2 // 100000000
b = a2 // 10000000 % 10
c = a2 // 1000000 % 10
d = a2 // 100000 % 10
e = a2 // 10000 % 10
f = a2 // 1000 % 10
g = a2 // 100 % 10
h = a2 // 10 % 10
b1 = a2 % 10
if a2 == a ** 9 + b ** 9 + c ** 9 + d ** 9 + e ** 9 + f ** 9 + g ** 9 + h ** 9 + b1 ** 9:
count6 +=1
pri6.append(a2)
print(f"有{count6}个九九重阳数")
print(pri6, end=' ')
print("是九九重阳数数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")
# 十全十美数
# 穷举法
stime = time.time()
for a3 in range(1000000000,10000000000):
a = a3 // 1000000000
b = a3 // 100000000 % 10
c = a3 // 10000000 % 10
d = a3 // 1000000 % 10
e = a3 // 100000 % 10
f = a3 // 10000 % 10
g = a3 // 1000 % 10
h = a3 // 100 % 10
b1 = a3 // 10 % 10
b2 = a3 % 10
if a3 == a ** 10 + b ** 10 + c ** 10 + d ** 10 + e ** 10 + f ** 10 + g ** 10 + h ** 10 + b1 ** 10 + b2 ** 10:
count7 +=1
pri7.append(a3)
print(f"有{count7}个十全十美数")
print(pri7, end=' ')
print("是十全十美数")
etime = time.time()
runtime = etime - stime
print(f"耗时{runtime}")