实验 2-4:逆序输出三位数
题目描述
编写程序,从键盘输入一个三位数,分离出个位数、十位数、百位数上的数字并逆序输出该三位数。
输入
一个三位数,例如:123
输出
逆序输出该三位数,例如:321
代码实现
san = int(input())
gge = san % 10
shhi = (san // 10) % 10
bbai = san // 100
print(100 * gge + 10 * shhi + bbai)
正确答案
x = eval(input(""))
bai = x // 100
shi = x // 10 % 10
ge = x % 10
print(ge * 100 + shi * 10 + bai)
测试用例
输入:123
输出:321
输入:654
输出:456
实验 2-5:统计决赛学生人数
题目描述
某高校举办程序设计大赛,输入学号名单,计算进入决赛的学生人数。输入的学号以逗号分隔。
输入
学号列表,例如:'J0806',' J0723',' B0605',' S0901',' Z0403',' A1010'
输出
输出两个数字,分别表示使用字符串和列表计算的结果,例如:6 6
s = input().split()
lst = list(s)
num1 = len(lst)
num2 = len(s)
print(num1, num2)
测试用例
输入:'J0806',' J0723',' B0605',' S0901',' Z0403',' A1010'
输出:6 6
实验 2-6:统计字母出现次数
题目描述
输入一句英文谚语,统计其中字母 w
或 W
出现的总次数。
输入
英文谚语,例如:'Where there is a Will, there is a way.'
输出
字母 w
或 W
出现的次数,例如:3
代码实现
saying = input()
final = 0
for i in range(len(saying)):
if saying[i] == 'w' or saying[i] == 'W':
final += 1
print(final)
正确答案
s = input()
ss = s.lower()
num = ss.count('w')
print(num)
测试用例
输入:'Where there is a Will, there is a way.'
输出:3
实验 2-7:按 ASCII 顺序排序字符串
题目描述
输入一个字符串,按 ASCII 顺序排序后输出,每个字符之间用空格分隔。
输入
字符串,例如:hello,world
输出
按 ASCII 顺序排序后的字符串,例如:, d e h l l l o o r w
代码实现
a = input()
b = sorted(a)
b = ' '.join(b)
print(b)
正确答案
s = input()
ss = sorted(s)
print(' '.join(ss))
测试用例
输入:hello,world
输出:, d e h l l l o o r w
输入:Where there is a Will
输出:W W a e e e e h h i i l l r r s t
实验 2-8:计算跳水选手得分
题目描述
输入 10 名评委的分数,去掉一个最高分和一个最低分,计算剩余分数的平均值,保留两位小数。
输入
10 个分数,例如:8.5,9,9,10,7,8,8,9,8,10
输出
选手的最终得分,例如:8.69
代码实现
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 = eval(input())
score = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10]
score.sort()
score.pop()
score.pop(0)
ave = sum(score) / len(score)
print(round(ave, 2))
正确答案
Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, Score9, Score10 = eval(input())
Scores = [Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, Score9, Score10]
Scores.sort()
Scores.pop()
Scores.pop(0)
aveScore = sum(Scores) / len(Scores)
print(round(aveScore, 2))
测试用例
输入:8.5,9,9,10,7,8,8,9,8,10
输出:8.69
输入:8.5,9,10,10,7,8,8,9,8,10
输出:8.81
实验 2-9:计算不及格和优秀学生的平均成绩
题目描述
输入一个学生成绩列表,分别计算不及格(小于 60 分)和优秀(大于等于 90 分)学生的平均成绩,保留两位小数。
输入
学生成绩列表,例如:[85,60,45,33,90,95,100]
输出
不及格学生平均成绩和优秀学生平均成绩,例如:39.0 95.0
代码实现
list1 = eval(input())
list1.sort()
low = list1.index(60)
high = list1.index(90)
lowl = list1[0:low]
highl = list1[high:]
avelow = sum(lowl) / len(lowl)
avehigh = sum(highl) / len(highl)
print(round(avelow, 1))
print(round(avehigh, 1))
正确答案
list1 = eval(input())
list2 = sorted(list1)
low = list2.index(60)
high = list2.index(90)
listlow = list2[0:low]
listhigh = list2[high:]
avglow = sum(listlow) / len(listlow)
avghigh = sum(listhigh) / len(listhigh)
print(round(avglow, 2))
print(round(avghigh, 2))
测试用例
输入:[85,60,45,33,90,95,100]
输出:39.0 95.0
输入:[90,100,60,78,87,66,54,32,82,66]
输出:43.0 95.0
实验 2-10:判断候选人是否获得选票
题目描述
输入一个候选人编号,判断其是否获得选票,输出 True
或 False
。
输入
候选人编号,例如:1
输出
是否获得选票,例如:True
代码实现
nam = list('4781226221687455585542264')
ans = input()
print(ans in nam)
正确答案
a = eval(input())
clist = [4, 7, 8, 1, 2, 2, 6, 2, 2, 1, 6, 8, 7, 4, 5, 5, 5, 8, 5, 5, 4, 2, 2, 6, 4]
c = set(clist)
print(a in c)
测试用例
输入:1
输出:True
输入:3
输出:False
实验 2-11:查询学生成绩
题目描述
输入一个学生成绩字典,查询学号为 J06
的学生分数,并输出所有学生的分数。
输入
学生成绩字典,例如:{'J01':88,'J02':60,'J03':80,'J04':96,'J05':86,'J06':75,'J07':76,'J08':82}
输出
学号为 J06
的学生分数和所有学生的分数,例如:75 dict_values([88, 60, 80, 96, 86, 75, 76, 82])
代码实现
dict = eval(input())
print(dict['J06'])
print(dict.values())
正确答案
PInfo = eval(input())
print(PInfo['J06'])
print(PInfo.values())
测试用例
输入:{'J01':88,'J02':60,'J03':80,'J04':96,'J05':86,'J06':75,'J07':76,'J08':82}
输出:75 dict_values([88, 60, 80, 96, 86, 75, 76, 82])
实验 2-12:查询月份天数
题目描述
输入一个月份编号,查询该月的天数。不考虑闰年,2 月按 28 天计算。
输入
月份编号,例如:3
输出
该月的天数,例如:31
代码实现
dict = {'1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31}
month = input()
print(dict[month])
正确答案
PInfo = {'1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31}
month = input()
print(PInfo[month])
测试用例
输入:3
输出:31
输入:2
输出:28
输入:4
输出:30