1.(分配成绩)编写一个程序,读取分数列表,然后分配 等级基于以下方案:
如果分数是最好的-10,则等级为 A 。
如果分数是最好的-20,则等级为 B。
如果分数是最好的-30,则等级为 C 。
如果分数是最好的-40,则等级为 D 。
否则等级为F
下面给出示例:输入40 55 70 58
输出:
student 0 score is 40 and grade is C
student 1 score is 55 and grade is B
student 2 score is 70 and grade is A
student 3 score is 58 and grade is B
scores=[int(s) for s in input("Enter scores seperated by spaces from one line:").split()]
n=len(scores)
if n!=0 and n!=1:
best=max(scores)
for i in range(n):
if scores[i]>=(best-10):
print("Student",i,"score is",scores[i],"and grade is A")
elif scores[i]>=(best-20) and scores[i]<(best-10):
print("Student",i,"score is",scores[i],"and grade is B")
elif scores[i]>=(best-30) and scores[i]<(best-20):
print("Student",i,"score is",scores[i],"and grade is C")
elif scores[i]>=(best-40) and scores[i]<(best-30):
print("Student",i,"score is",scores[i],"and grade is D")
else:
print("Student",i,"score is",scores[i],"and grade is F")
elif n==1:
print("Student 0 score is",scores[i],"and grade is A")
2.(计算数字的出现次数)编写一个读取一些整数的程序
1 到 100 之间,并计算每个出现的次数。 这是一个示例运行
该程序:
输入:2 5 6 5 4 3 23 43 2
输出:
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs time
43 occurs 1 time
list=[int(s)for s in input().split()]
list.sort() # 将列表进行排序
sum=[]#定义一个用来记录每个数出现次数的列表
for i in range(101):
sum.append(0)#对sum列表进行初始化
for i in range(len(list)):#对列表list进行遍历
sum[list[i]]+=1#对于出现的数字i,他的计数数组sum[i]+1
t=list[-1]#对列表list进行去重
for i in range(len(list)-2,-1,-1):
if t==list[i]:
list .remove(list[i])
else :
t=list[i]
for i in range(101):#从0到100遍历
if sum[i] >= 2:#当数字i出现的次数大于等于2时
print(i,"occurs",sum[i],"times")
elif sum[i]==1:#当数字i只出现一次时
print(i,"occurs 1 time")
3.(打印不同的数字)编写一个程序,输入一些数,输出没有重复的数,如果一个数出现多次则输出一次即可,这是程序的示例运行:
输入:1 2 3 2 1 6 3 4 5 2
输出:1 2 3 6 4 5
list=[int(s)for s in input().split()]
n=len(list)-1#n表示列表的最大下标
for i in range(n-1):#i只遍历到n-1是为了防止遍历j的时候发生越界
for j in range(i+1,n):
if list[i]==list[j]:
for k in range(j,n-1):#k只遍历到n-1也是防止越界
list[k]=list[k+1]
i=0
n=n-1#每删一个数,列表长度减一
for i in range(n):
print(list[i],end=" ")