Skip to content

Commit a928743

Browse files
authored
Merge pull request #5156 from CHJ219/master
【1901100231】自学训练营学习18群-Day 5
2 parents 1662ba6 + 0ccbe70 commit a928743

File tree

5 files changed

+137
-24
lines changed

5 files changed

+137
-24
lines changed

exercises/1901100231/1001S02E03_calculator.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
#告知这是个计算机小程序
2+
a="1"
3+
24
print("""请键入要进行运算的两个数字
3-
并按:
4-
“+”代表加
5-
“-”代表减
6-
“*”代表乘
7-
“/”代表除
8-
“%”代表求余数
9-
“**”代表求次方
10-
的如上所示的规则键入你要进行的运算
11-
本程序只可运行一次,重复计算请重复使用
12-
请切换至英文输入模式进行数字和运算的键入""")
13-
#获取要算的数和运算
14-
x = input()
15-
y = input()
16-
z = input()
5+
并按:
6+
“+”代表加
7+
“-”代表减
8+
“*”代表乘
9+
“/”代表除
10+
“%”代表求余数
11+
“**”代表求次方
12+
的如上所示的规则键入你要进行的运算
13+
本程序可多次使用,重复上述过程即可
14+
请切换至英文输入模式进行数字和运算的键入""")
15+
#获取要算的数和运算
16+
while a=="1":
17+
x = input("请输入第一个数字")
18+
y = input("请输入第二个数字")
19+
z = input("请输入要进行的运算类型")
1720
#计算器程序
18-
if z == "+":
21+
if z == "+":
1922
print(x,"+",y,"=",int(x)+int(y))#加法
20-
elif z== "-":
23+
elif z== "-":
2124
print(x,"-",y,"=",int(x)-int(y))#减法
22-
elif z== "*":
25+
elif z== "*":
2326
print(x,"*",y,"=",int(x)*int(y))#乘法
24-
elif z== "/":
27+
elif z== "/":
2528
print(x,"/",y,"=",int(x)/int(y))#除法
26-
elif z== "%":
29+
elif z== "%":
2730
print(x,"除以",y,"的余数是",int(x)%int(y))#余数
28-
elif z== "**":
31+
elif z== "**":
2932
print(x,"的",y,"次方是",int(x)**int(y))#次方
3033

3134

exercises/1901100231/1001S02E04_control_flow.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
查看while函数打印九九乘法表(已去掉偶数行)请输入2
33
请在英文输入模式下输入
44
''')#对使用者提供的解释
5-
y="1"
5+
y = "1"
66
while y =="1":#使程序重复运行
77
x = input("请输入:")#获取要进行的操作
88
if x=="1":#for...in...函数打印九九乘法表
9-
for i in range(1,10):#第一个数
9+
for i in range(1,10):#第一个数
1010
for j in range(1,i+1):#第二个数
1111
print(i,"*",j,"=",int(i)*int(j),end="\t")#打印乘法表,并利用制表符使每一列对齐
1212
print()#每遍历完一行执行一次换行操作
@@ -22,8 +22,7 @@
2222
a=a+1
2323
b=int(1)
2424
else:#若是偶数行,则令a的值再次加一,从while部分重新开始运行程序
25-
a=a+1
26-
y=input("请问是否重复运行程序,若是请输入1:")
25+
a=a+1
2726

2827

2928

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
a = [0,1,2,3,4,5,6,7,8,9]#用列表创建数组
2+
a.reverse()#翻转其中的元素
3+
b = ' '.join(str (x) for x in a)#转换为字符串,且将数字类型转换为字符类型
4+
b = b.replace(" ","")#要去除空格,因为空格也会占据一个索引位置
5+
c = b[2:8]#取出指定索引对应的数字
6+
c = c[7::-1]#用反向切片进行反转(转换为列表反转麻烦一点,而且不符合任务要求。)
7+
c = c.replace('',' ')#重新将空格还原
8+
c = c.strip()#去除两端的空格
9+
a = c.split(' ')#将c中数字转换回列表a中
10+
a = [int(x) for x in a ]#将字符转换为整形,否则无法进行进制的转换
11+
print("十进制:",a)
12+
o = [bin(x) for x in a ]
13+
print("二进制:",o)
14+
p = [oct(x) for x in a ]
15+
print("八进制:",p)
16+
q = [hex(x) for x in a ]
17+
print("十六进制:",q)#不知道原因,为何在进行了二进制的转换后无法直接转换为八进制和十六进制,故另设了o,p,q三个列表用来分别存储转换进制后a中的数字
18+
19+
20+
21+
22+
23+
24+
25+
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
text = '''
2+
The Zen of Python, by Tim Peters
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambxiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do
16+
it.
17+
Although that way may not be obvious at first unless you're Dutch.
18+
Now is better than never.
19+
Although never is often better than *right* now.
20+
If the implementation is hard to explain, it's a bad idea.
21+
If the implementation is easy to explain, it may be a good idea.
22+
Namespaces are one honking great idea -- let's do more of those!
23+
'''#待处理文本
24+
text=text.lower()
25+
#统一为小写
26+
text=text.replace("\n"," ")#换行符不能直接替换为无,否则行末单词会和行首单词相连,影响后续统计
27+
text=text.replace("!","")
28+
text=text.replace(".","")
29+
text=text.replace(",","")
30+
text=text.replace("--","")
31+
text=text.replace("*","")
32+
#去除换行符和其他符号
33+
copy = text.split(' ')#老规矩,先转换为列表,再进行统计
34+
copy = [x for x in copy if x !=""]#去除空格,否则后续排序时会出现不明空格元素
35+
a={}#创建空字典
36+
for x in copy:
37+
if x in a:
38+
a[x]=a[x]+1
39+
else:
40+
a[x]=1
41+
for x in sorted(a,key=a.__getitem__,reverse=True):#反向排序
42+
print(x,a[x])
43+
44+
45+
46+
47+
48+
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
text = '''
2+
The Zen of Python, by Tim Peters
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambxiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do
16+
it.
17+
Although that way may not be obvious at first unless you're Dutch.
18+
Now is better than never.
19+
Although never is often better than *right* now.
20+
If the implementation is hard to explain, it's a bad idea.
21+
If the implementation is easy to explain, it may be a good idea.
22+
Namespaces are one honking great idea -- let's do more of those!
23+
'''#要处理的文本
24+
text = text. replace ("better","worse")#用replace函数进行关键词的替换
25+
text = text. replace ("\n"," ")#去除换行符,使结果更美观
26+
text = text. replace ("*","")
27+
text = text. replace ("--","")
28+
text = text. replace (".","")#去除其他符号,否则会影响后续统计
29+
copy = text.split(' ')#将字符串转化为列表,便于之后的操作
30+
copy = [x for x in copy if x != '']#去除列表中的空格元素,否则总是有空格会参与排序
31+
copy = [x for x in copy if "ea" not in x]#找出copy中的含“ea”的元素并删去,用循环加条件判断会有漏网之鱼,原因不明
32+
text = ' '.join(copy)#由于大小写转换命令只适用于字符串,故转换回字符串
33+
text=text.swapcase()#进行大小写互换
34+
copy = text.split(' ')#为了排序,在不更改任务顺序的前提下再次转化为列表进行排序
35+
copy.sort()
36+
print(copy)
37+

0 commit comments

Comments
 (0)