一、以五分制绩点为基础的绩点计算器
1.1 效果演示
计算绩点截图:
功能:可以录入成绩,查询科目,删除某个科目,修改某个科目,等等功能
1.2 完整源代码
'''
功能:可以录入成绩,查询科目,删除某个科目,修改某个科目,等等功能
## <font color="#4B5CC4" size=4>1.2 完整源代码</font>
```python
'''
author:kenwanmao
绩点计算器
'''
import os
filename = 'course.txt'
#主函数
def main():
while True:
menu()
choice = int(input('请选择功能:'))
choice_list = {}
if choice in [0,1,2,3,4,5,6,7]:
if choice ==0 :
answer = input('你确定要退出系统吗?y/n:')
if answer =='y' or answer =='Y':
print('thanks for using!!')
break #退出系统
else:
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
count()
elif choice ==7:
show()
#显示菜单
def menu():
print('=============绩点计算系统============')
print('---------------功能菜单--------------')
print('\t\t1.录入成绩')
print('\t\t2.查找科目')
print('\t\t3.删除科目')
print('\t\t4.修改科目成绩')
print('\t\t5.排序')
print('\t\t6.计算绩点')
print('\t\t7.显示所有科目信息')
print('\t\t0.退出系统')
print('------------------------------------')
#插入课程
def insert():
course_list =[]
while True:
name = input('请输入科目名字:')
if not name:
print('请输入科目名字!')
break
try:
credit = int(input('请输入该科目的学分:'))
performance = int(input('请输入该科目你所获得的分数:'))
except:
print('请输入正确的科目学分或者课程分数!')
continue
course = {'name':name,'credit':credit,'performance':performance}
course_list.append(course)
answer = input('你还要继续添加科目吗? y/n :')
if answer =='y' or answer =='Y':
continue
else:
break
saveinfo(course_list)
print('课程信息录入完毕!')
#搜索课程
def search():
course_query=[]
while True:
if os.path.exists(filename):
name = input('请输入科目的名称来查询科目信息:')
with open(filename,'r',encoding='utf-8') as rfile:
course = rfile.readlines()
for item in course:
d = dict(eval(item))
if d['name'] == name:
course_query.append(d)
show_course(course_query)
course_query.clear()
answer = input('是否继续查询?y/n:')
if answer == 'y' or answer =='Y':
continue
else:
break
else:
print('查询不到该科目\n')
return
#计算课程总数
def total():
if os.path.exists(filename):
with open(filename,'r', encoding='utf-8') as rfile:
course_list = rfile.readlines()
if course_list:
print('科目总数为{}\n'.format(len(course_list)))
else:
print('科目总数为0\n')
return
else:
print('暂时未保存任何科目信息\n')
#对课程进行排序
def sort():
show()
if os.path.exists(filename):
with open(filename,'r', encoding='utf-8') as rfile:
course_list = rfile.readlines()
course_new = []
for item in course_list:
d = dict(eval(item))
course_new.append(d)
else:
return
mode = input('请选择排序方式:1.按成绩从高到低进行排序,2.按学分从大到小进行排序, 3.退出排序:')
if mode =='1':
course_new.sort(key= lambda x :int(x['performance']),reverse=True)
elif mode == '2':
course_new.sort(key= lambda x :int(x['credit']),reverse=True)
elif mode =='3':
return
else:
print('输入有误,请重新输入\n')
sort()
print('排序后的结果:')
show_course(course_new)
#保存课程信息
def saveinfo(list):
try:
course_txt = open(filename,'a',encoding='utf-8')
except:
course_txt = open(filename,'w',encoding='utf-8')
for item in list:
course_txt.write(str(item)+'\n')
course_txt.close()
#删除课程信息
def delete():
while True:
course_name = input('请输入需要删除的科目名称:')
if course_name != '':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as file:
course_old = file.readlines()
else:
course_old = []
flag = False
if course_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={}
for item in course_old:
d = dict(eval(item)) #将字符串转成字典
if d['name'] != course_name:
wfile.write(str(d)+'\n')
else:
flag = True
if flag:
print('科目{}已经被删除\n'.format(course_name))
else:
print('没有找到科目{}可删除\n'.format(course_name))
else:
print('无科目信息可供删除\n')
break
show()
answer = input('是否继续删除呢?y/n:')
if answer == 'y' or answer =='Y':
continue
else:
break
#编辑课程信息
def modify():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
course_old = rfile.readlines()
else:
return
course_name = input('请输入需要修改的科目名称:')
with open(filename, 'w', encoding='utf-8') as wfile:
for item in course_old:
d = dict(eval(item))
if d['name'] == course_name:
print('找到该科目了')
while True:
try:
d['name'] = input('请输入新的科目名字:')
d['credit'] = int(input('请输入该科目新的学分:'))
d['performance'] = int(input('请输入该科目你所获得的新分数:'))
except:
print('您的输入有误,请重新输入\n')
else:
break
wfile.write(str(d)+'\n')
print('修改成功!\n')
else:
wfile.write(str(d) + '\n')
answer = input('是否继续修改呢?y/n:')
if answer == 'y' or answer == 'Y':
modify()
#计算绩点的函数
def count():
if os.path.exists(filename):
with open(filename,'r', encoding='utf-8') as rfile:
course_count = rfile.readlines()
total_performance =0
total_credit =0
for item in course_count:
d = dict(eval(item))
performance = int(d['credit']) * int(d['performance'])
total_performance += performance
total_credit += int(d['credit'])
print('经计算,您的学分加权平均分为:{:.2f}'.format(total_performance/total_credit))
gpa_average =(total_performance/total_credit-50)/10
print('经计算,您的平均学分绩点为:{:.2f}\n'.format(gpa_average))
else:
print('暂无科目信息\n')
return
#显示全部课程信息
def show():
course_list =[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
courses = rfile.readlines()
for item in courses:
course_list.append(eval(item))
if course_list :
show_course(course_list)
total()
else:
print('暂未保存任何科目信息')
#用于输出课程信息
def show_course(course_list):
if len(course_list)==0:
print('没有查询到该科目')
return
for item in course_list:
print('科目名称:{},科目学分:{},科目成绩:{}'.format(item.get('name'),
item.get('credit'),item.get('performance')))
if __name__ == '__main__':
main()
模仿练习,python入门,勿喷。
参考博客
1.Python 文件操作中的读写模式:open(path, ‘-模式-’,encoding=‘UTF-8’)
2.花了2万多买的Python教程全套,现在分享给大家,入门到精通(Python全栈开发教程)
3.python格式化输出保留2位小数