19
19
del (str1 [0 ])
20
20
21
21
list1 = [1 , 2 , 3 ]
22
+ list1 .append (4 )
23
+ list1 .insert (0 ,3 )
24
+ result = list1 .pop ()
25
+ del (list1 [0 ])
26
+ list1 [0 ] = 4
22
27
print (list1 [:])
23
28
print (list1 [0 ])
24
- del (list1 [0 ])
29
+
30
+ set1 = {1 , 2 , 3 , 1 }
31
+ print (set1 )
32
+
33
+ dict1 = {"name" : "geyang" , "age" : 24 }
34
+ dict1 ["height" ] = 173
35
+ del (dict1 ["height" ])
36
+ dict1 ["height" ] = 174
37
+ result = dict1 .get ("heihgt" )
25
38
# 添加元素
26
39
# 元组不可以被修改,包括添加元素,任何方法都不可以
27
40
#tuple1[3] = 4 # 'tuple' object does not support item assignment
@@ -94,9 +107,12 @@ def count(day):
94
107
# 考试题2:一次性输入任意不少于5个数字(数字之间以逗号为界)保存在list中,
95
108
# 对输入的数字进行排序,不准使用内置函数,封装成函数
96
109
def sort_number ():
97
- input_str = input ()
110
+ input_str = input ("请输入不少于5个数字(格式:5,3,1,2)" )
98
111
number_list = input_str .split ("," )
99
112
# print(number_list)
113
+ if len (number_list ) < 5 :
114
+ print ("输入数字不能少于5个" )
115
+ return
100
116
number_list = [ int (index ) for index in number_list ]
101
117
print ("before sort:" , number_list )
102
118
# 冒泡排序
@@ -129,7 +145,7 @@ def slice_list():
129
145
while True :
130
146
if index == len (input_list ):
131
147
break
132
- while input_list [index ] == input_list [index + 1 ]:
148
+ while ( index != len ( input_list ) - 2 ) and input_list [index ] == input_list [index + 1 ]:
133
149
temp_list .append (input_list [index ])
134
150
index += 1
135
151
temp_list .append (index )
@@ -141,7 +157,25 @@ def slice_list():
141
157
142
158
# 考试题4:写一个程序,可以任意输入文件名,打开该文件,如果文件不存在则创建该文件。
143
159
# 然后输入内容,可重复输入追加到文件中。
144
-
160
+ def write_file ():
161
+ file_name = input ("请输入文件名:" )
162
+ # 打开文件
163
+ try :
164
+ # "E:\workspace_python\README.md"
165
+ f = open (file_name , 'a+' )
166
+ while True :
167
+ input_str = input ()
168
+ if input_str == '#' :
169
+ print (f .read ())
170
+ break
171
+ else :
172
+ f .write (input_str )
173
+ except FileNotFoundError :
174
+ print ("没有这个文件!" )
175
+ finally :
176
+ f .close ()
177
+
178
+ write_file ()
145
179
# 考试题5:计算年龄
146
180
#f(1) = 10 f(2) = f(1) + 2 f(3) = f(2) + 2
147
181
def count_age (number ):
@@ -206,25 +240,60 @@ class Class(object):
206
240
def __init__ (self , person_number ):
207
241
Class .__person_number = person_number
208
242
209
- def increase_person_number (self ):
210
- pass
243
+ def increase (self ):
244
+ Class . __person_number += 1
211
245
212
246
def get_peson_number (self ):
247
+ print ("班级总人数为:%d" % Class .__person_number )
213
248
return Class .__person_number
214
-
249
+ #class1 = Class(10)
250
+ #print(class1.get_peson_number())
251
+ #class1.increase()
252
+ #print(class1.get_peson_number())
215
253
class Student (Class ):
216
254
217
- def __init__ (self , name , age , score ):
255
+ def __init__ (self , name , age , score , myClass ):
218
256
self .name = name
219
257
self .age = age
220
258
self .score = score
259
+ myClass .increase ()
221
260
222
261
def get_name (self ):
262
+ print ("大家好我叫%s" % self .name )
223
263
return self .name
224
264
225
265
def get_age (self ):
226
- return self .age
266
+ print ("姓名:%s年龄:%d" % (self .name , self .age ))
267
+ return self .name ,self .age
227
268
228
269
def get_score (self , class_name ):
229
270
if class_name in self .score :
230
- return "%d:%d" % (class_name , self .score [class_name ])
271
+ print ("这是我的%s:%d" % (class_name , self .score [class_name ]))
272
+ return class_name , self .score [class_name ]
273
+
274
+ def get_best_score (self ):
275
+ max_score = 0
276
+ course = ""
277
+ for k ,v in self .score .items ():
278
+ if v > max_score :
279
+ max_score = v
280
+ course = k
281
+ print ("我最好的成绩是%s:%d" % (course , max_score ))
282
+ return max_score , course
283
+
284
+ class1 = Class (0 )
285
+ joe = Student ("joe" , 20 , {"语文" :69 , "数学" : 70 , "英语" : 100 }, class1 )
286
+ joe .get_name ()
287
+ joe .get_age ()
288
+ joe .get_score ("语文" )
289
+ joe .get_best_score ()
290
+ print ('-' * 20 )
291
+ susan = Student ("susan" , 20 , {"语文" : 99 , "数学" : 100 , "英语" :79 }, class1 )
292
+ susan .get_name ()
293
+ susan .get_age ()
294
+ susan .get_score ("语文" )
295
+ susan .get_best_score ()
296
+ print ('-' * 20 )
297
+ class1 .get_peson_number ()
298
+
299
+ 12
0 commit comments