#题目要求
#我的代码
def read_file(filename):
"""读文件,将数据切分为列表,返回二维列表"""
with open(filename, 'r', encoding='utf-8') as fr:
data_ls = [i.strip().split(',') for i in fr]
return data_ls[1:]
def rank_four(data_ls):
"""接受录取概率大于num的数据的二维列表,
计算并返回其中大学排名评分大于等于4分的百分比"""
admit_80 = [x for x in data_ls if float(x[-1])>=0.8]
top_four = [x for x in admit_80 if float(x[1])>=4]
percent = round(len(top_four) / len(admit_80),4)*100
print(f'Top University in >=80%:{percent}%')
# return f'Top University in >=80%:{percent}%'
def toefl(data_ls):
admit_80 = [x for x in data_ls if float(x[-1])>=0.8]
toefl_score = [int(x[3]) for x in admit_80]
avg_toefl = sum(toefl_score)/len(toefl_score)
print(f'TOEFL Average Score:{avg_toefl:.2f}')
print(f'TOEFL Max Score:{max(toefl_score):.2f}')
print(f'TOEFL Min Score:{min(toefl_score):.2f}')
def gpa(data_ls):
admit_80 = [x for x in data_ls if float(x[-1])>=0.8]
gpa_score = [float(x[4]) for x in admit_80]
avg_toefl = sum(gpa_score)/len(gpa_score)
print(f'CGPA Average Score:{avg_toefl:.3f}')
print(f'CGPA Max Score:{max(gpa_score):.3f}')
print(f'CGPA Min Score:{min(gpa_score):.3f}')
def graduate_student(data_ls):
admit_90 = [x for x in data_ls if float(x[-1])>=0.9]
gpa_score = [x[5] for x in admit_90 if x[5]=='1']
percent = len(gpa_score)/len(admit_90)*100
print(f'Reseach in >=90%:{percent:.2f}%')
admit_90 = [x for x in data_ls if float(x[-1]) <= 0.7]
gpa_score = [x[5] for x in admit_90 if x[5] == '1']
percent = len(gpa_score) / len(admit_90) * 100
print(f'Reseach in <=70%:{percent:.2f}%')
def type_judge(input_str):
"""接收一个字符串为参数,根据参数调用不同的函数进行运算。
"""
if input_str == '1':
rank_four(data)
elif input_str == '2':
toefl(data)
elif input_str == '3':
gpa(data)
elif input_str == 'Research':
graduate_student(data)
else:
print('ERROR')
if __name__ == '__main__':
file = 'admit2.csv'
data = read_file(file)
question = input()
type_judge(question)
#遇到的问题
上述题目要求中的情况4,不能正常运行、输出结果。请问如何解决?