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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
''''''
import random

'''

初级:
1.已知字符串:“this is a test of Python”
a.统计该字符串中字母s出现的次数
b.取出子字符串“test” 提示: str.find('test')
c.采用不同的方式将字符串倒序输出
d.将其中的"test"替换为"exam" str.replace()
'''
# a.统计该字符串中字母s出现的次数
s = "this is a test of Python"
count = 0
for c in s:
if c == 's':
count += 1
print(count)

# b.取出子字符串“test” 提示: str.find('test')
s = "this is a test of Python"
start = s.find('test')
end = start + len('test')
print( s[start:end] )

# c.采用不同的方式将字符串倒序输出
s = "this is a test of Python"
print(s[::-1])

s2 = ""
for i in range(len(s)-1, -1, -1):
s2 += s[i]
print(s2)

s3 = ""
for c in s:
s3 = c + s3
print(s3)

# d.将其中的"test"替换为"exam" str.replace()
s = "this is a test of Python"
s2 = "exam"
s3 = s.replace('test', s2)
print(s) # 'this is a test of Python'
print(s3) # 'this is a exam of Python'


'''
2.已知字符串 a = "aAsmr3idd4bgs7Dlsf9eAF",要求如下
a.请将a字符串的大写改为小写,小写改为大写 ch.lower()小写, ch.upper()大写
b.请将a字符串的数字取出,并输出成一个新的字符串 ch>='0' and ch<='9'
c.请统计a字符串出现的每个字母的出现次数(忽略大小写,a与A是同一个字母),
并输出成一个字典。 例 {'a':4,'b':2}
d.输出a字符串出现频率最高的字母
e.请判断'boy'里出现的每一个字母,是否都出现在a字符串里。
如果出现,则输出True,否则,则输 出False

'''
a = "aAsmr3idd4bgs7Dlsf9eAF"

# a.请将a字符串的大写改为小写,小写改为大写 提示: ch.lower()小写, ch.upper()大写
a2 = ""
for c in a:
# 小写字母
if c>='a' and c<='z':
a2 += c.upper()
# 大写字母
elif c>='A' and c<='Z':
a2 += c.lower()
else:
a2 += c

print(a2) # AaSMR3IDD4BGS7dLSF9Eaf

# b.请将a字符串的数字取出,并输出成一个新的字符串 ch>='0' and ch<='9'
a = "aAsmr3idd4bgs7Dlsf9eAF"
a3 = ""
for c in a:
# 数字
if c>='0' and c<='9':
a3 += c

print(a3)


# c.请统计a字符串出现的每个字母的出现次数(忽略大小写,a与A是同一个字母),
# 并输出成一个字典。 例 {'a':4,'b':2}
a = "aAsmr3idd4bgs7Dlsf9eAF"
d = {}

for c in a:
# 判断是否为字母
if c>='a' and c<='z' or c>='A' and c<='Z':
# 转换成小写
c2 = c.lower()

#
if c2 not in d:
d[c2] = 1
else:
d[c2] += 1

print(d)

# d.输出a字符串出现频率最高的字母
l = list(d.values())
# print(l, max(l))
max_count = max(l) # 最大次数

for k,v in d.items():
if v == max_count:
print(k, end=', ')

print()
# e.请判断'boy'里出现的每一个字母,是否都出现在a字符串里。
# 如果出现,则输出True,否则,则输 出False
a = "aAsmr3idd4bgs7Dlsf9eAF"

for c in "boy":
if c not in a:
print(False)
break
else:
print(True)


'''
注:能不使用系统功能的尽量不要使用,自己实现
中级
1.去除字符串两端的指定字符.
s = "hello"
ch = "h"
# ello
'''
s = "hhhhhelhhlohhhhhh"
ch = 'h'

i = 0
while i < len(s):
if s[i] == ch:
s = s[1:]
i -= 1
else:
break
i += 1
print(s) # elhhlohhhhhh

i = len(s) - 1
while i >= 0:
if s[i] == ch:
s = s[:-1]
else:
break
i -= 1

print(s) # elhhlo


'''
2.键盘输入一句英文 将每个单词的首字母大写
例如:
输入: hello nice to meet you
转化之后: Hello Nice To Meet You

'''
s = "hello nice to meet you"
s2 = ""
for i in range(len(s)):
if i==0 or s[i-1]==" ":
s2 += s[i].upper()
else:
s2 += s[i]

print(s2) # Hello Nice To Meet You

'''
3.输入一个字符串,压缩字符串如下aabbbccccd变成a2b3c4d1
'''
s = "aabbbccccd"
d = {}
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1

print(d) # {'a': 2, 'b': 3, 'c': 4, 'd': 1}

s2 = ""
for k, v in d.items():
s2 = s2 + k + str(v)

print(s2) # a2b3c4d1


'''

4.完成猜拳游戏
-----------------------------
请输入你的选择:
1. 石头
2. 剪刀
3. 布
-----------------------------
你的选择是【布】, 电脑的选择是【石头】
恭喜你获得了胜利!


'''

print('''-----------------------------
请输入你的选择:
1. 石头
2. 剪刀
3. 布
-----------------------------''')

while True:
me = int(input("请出拳(输入编号):"))
computer = random.randint(1,3)
print(me, computer)

if me-computer == -1 or me-computer == 2:
print("恭喜你获得胜利!")
break
elif me == computer:
print("平手")
else:
print("我输了")