a = {'张三':45,'李思':78,'王吴':40,'周六':96,'赵琪':65,'孙霸':90,'郑九':78,'吴诗':99,'董世义':60}
ages = [a[k] for k in a]
mx = max(ages)
mn = min(ages)
avg = sum(ages) / len(ages)
grade = 'abcde'[::1]
print('最高分:{},最低分:{},平均分:{}'.format(mx,mn,round(avg,1)))
for k in a:
print('{} 成绩为 {}'.format(k,grade[a[k] // 20]))
a = [3,7,1,23,45,21,12]
b = (0,2,33,55,11)
a += b
n = int(input())
if a.count(n) > 0:
print(a.index(n))
else:
print('n is not find')
3:
代码:
class Student(object):
def __init__(self,name,score):
self._name = name
self._score = score
def getscore(self):
return self._score
def getname(self):
return self._name
def showAE(self):
print('%s:%d:'%(self._name,self._score),end='')
if self._score >=90:
print('A')
elif self._score >=80:
print('B')
elif self._score >=70:
print('C')
elif self._score >=60:
print('D')
else:
print('E')
ls=[]
ls.append(Student('张三',45))
ls.append(Student('李思',78))
ls.append(Student('王吴',40))
ls.append(Student('周六',96))
ls.append(Student('赵琪',65))
ls.append(Student('孙霸',90))
ls.append(Student('郑九',78))
ls.append(Student('吴诗',99))
ls.append(Student('董世义',60))
minScore = ls[0].getscore() #最高分
maxScore = ls[0].getscore() #最低分
ave = 0 #平均分
for i in range(0,len(ls)):
if ls[i].getscore() > maxScore:
maxScore = ls[i].getscore()
if ls[i].getscore() < minScore:
minScore = ls[i].getscore()
ave = ave + ls[i].getscore()
ave = ave / len(ls) #求平均分
print('最高分:%d,最低分:%d,平均分:%f'%(maxScore,minScore,ave))
# A-E输出
for i in range(0,len(ls)):
ls[i].showAE()
4.
代码:
ls1=[3,7,1,23,45,21,12]
ls2=[0,2,33,55,11]
ls1 += ls2
n = int(input('n='))
flag = False #记录是否找到n
for i in range(0,len(ls1)):
if ls1[i] == n:
if flag == False:
print(i) # 输出所有n的下标
else:
print(' %d'%i) # 输出所有n的下标
flag = True
if flag == False:
print("%d is not find"%n)
scores = [
{"name": "张三", "score": 45},
{"name": "李思", "score": 78},
{"name": "王吴", "score": 40},
{"name": "周六", "score": 96},
{"name": "赵琪", "score": 65},
{"name": "孙霸", "score": 90},
{"name": "郑九", "score": 78},
{"name": "吴诗", "score": 99},
{"name": "董世义", "score": 60},
]
# 求最高分、最低分和平均分
scores_sorted = sorted(scores, key=lambda x: x["score"])
highest_score = scores_sorted[-1]["score"]
lowest_score = scores_sorted[0]["score"]
average_score = sum([s["score"] for s in scores]) / len(scores)
print("最高分:", highest_score)
print("最低分:", lowest_score)
print("平均分:", average_score)
# 将成绩从百分制转换成a-e制
def score_to_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "E"
for s in scores:
s["grade"] = score_to_grade(s["score"])
print(scores)
第二题
# 给定列表
my_list = [3, 7, 1, 23, 45, 21, 12]
# 要插入的元素
new_elements = [0, 2, 33, 55, 11]
# 在原地插入元素
my_list.extend(new_elements)
# 要查找的元素
n = 33
# 查找元素n在列表中的下标位置
if n in my_list:
index = my_list.index(n)
print("元素n在列表中的下标位置为:", index)
else:
print("n is not find")
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!