用Python编写程序,模拟实现一个四则运算练习模拟系统。用户分为老师用户和学生用户。老师用户登录后,可以管理题目信息和学生信息,并统计成绩;学生登录后,可随机在题库抽取N个题目完成题目练习,并可查看本次成绩或之前的所有成绩。要求所有信息都保存在d:\\temp\\unit9文件中。
时间: 2024-03-10 14:46:53 浏览: 61
Python实现加载用户程序的监控程序.zip
以下实现该功能的Python代码:
```python
import os
import random
# 检查文件夹和文件是否存在,不存在则创建
if not os.path.exists("d:\\temp\\unit9"):
os.mkdir("d:\\temp\\unit9")
filename = "d:\\temp\\unit9\\exercise.txt"
if not os.path.exists(filename):
file = open(filename, "w")
file.write("1+1=2\n")
file.write("2+2=4\n")
file.write("3+3=6\n")
file.write("4+4=8\n")
file.write("5+5=10\n")
file.close()
filename = "d:\\temp\\unit9\\score.txt"
if not os.path.exists(filename):
file = open(filename, "w")
file.close()
# 登录界面
while True:
print("=" * 30)
print("欢迎使用四则运算练习模拟系统")
print("1.老师用户登录")
print("2.学生用户登录")
print("3.退出系统")
choice = input("请选择:")
if choice == "1":
# 老师用户登录
print("=" * 30)
print("欢迎老师用户登录")
username = input("请输入用户名:")
password = input("请输入密码:")
if username == "admin" and password == "123456":
while True:
print("=" * 30)
print("请选择操作:")
print("1.添加题目")
print("2.删除题目")
print("3.修改题目")
print("4.查看题目")
print("5.添加学生")
print("6.删除学生")
print("7.修改学生")
print("8.查看学生")
print("9.统计成绩")
print("10.返回上一级菜单")
choice = input("请选择:")
if choice == "1":
# 添加题目
question = input("请输入题目内容:")
file = open("d:\\temp\\unit9\\exercise.txt", "a")
file.write(question + "\n")
file.close()
print("题目已添加。")
elif choice == "2":
# 删除题目
file = open("d:\\temp\\unit9\\exercise.txt", "r")
lines = file.readlines()
file.close()
file = open("d:\\temp\\unit9\\exercise.txt", "w")
question = input("请输入要删除的题目:")
deleted = False
for line in lines:
if line.strip() == question:
deleted = True
print("题目已删除。")
else:
file.write(line)
file.close()
if not deleted:
print("未找到要删除的题目。")
elif choice == "3":
# 修改题目
file = open("d:\\temp\\unit9\\exercise.txt", "r")
lines = file.readlines()
file.close()
file = open("d:\\temp\\unit9\\exercise.txt", "w")
question = input("请输入要修改的题目:")
modified = False
for line in lines:
if line.strip() == question:
modified = True
new_question = input("请输入新的题目内容:")
file.write(new_question + "\n")
print("题目已修改。")
else:
file.write(line)
file.close()
if not modified:
print("未找到要修改的题目。")
elif choice == "4":
# 查看题目
file = open("d:\\temp\\unit9\\exercise.txt", "r")
lines = file.readlines()
file.close()
for line in lines:
print(line.strip())
elif choice == "5":
# 添加学生
student = input("请输入学生姓名:")
file = open("d:\\temp\\unit9\\score.txt", "a")
file.write(student + ":0\n")
file.close()
print("学生已添加。")
elif choice == "6":
# 删除学生
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
file = open("d:\\temp\\unit9\\score.txt", "w")
student = input("请输入要删除的学生姓名:")
deleted = False
for line in lines:
if line.startswith(student + ":"):
deleted = True
print("学生已删除。")
else:
file.write(line)
file.close()
if not deleted:
print("未找到要删除的学生。")
elif choice == "7":
# 修改学生
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
file = open("d:\\temp\\unit9\\score.txt", "w")
student = input("请输入要修改的学生姓名:")
modified = False
for line in lines:
if line.startswith(student + ":"):
modified = True
new_score = input("请输入新的成绩:")
file.write(student + ":" + new_score + "\n")
print("学生已修改。")
else:
file.write(line)
file.close()
if not modified:
print("未找到要修改的学生。")
elif choice == "8":
# 查看学生
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
for line in lines:
print(line.strip())
elif choice == "9":
# 统计成绩
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
total_score = 0
for line in lines:
score = int(line.strip().split(":")[1])
total_score += score
print("总成绩为:", total_score)
elif choice == "10":
# 返回上一级菜单
break
else:
print("输入有误,请重新输入。")
else:
print("用户名或密码错误,请重新登录。")
elif choice == "2":
# 学生用户登录
print("=" * 30)
print("欢迎学生用户登录")
username = input("请输入用户名:")
password = input("请输入密码:")
if os.path.exists("d:\\temp\\unit9\\score.txt"):
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
for line in lines:
if line.startswith(username + ":"):
pwd = line.strip().split(":")[1]
if password == pwd:
print("登录成功。")
while True:
print("=" * 30)
print("请选择操作:")
print("1.开始练习")
print("2.查看本次成绩")
print("3.查看所有成绩")
print("4.返回上一级菜单")
choice = input("请选择:")
if choice == "1":
# 开始练习
file = open("d:\\temp\\unit9\\exercise.txt", "r")
lines = file.readlines()
file.close()
num = int(input("请输入要抽取的题目数量:"))
random_lines = random.sample(lines, num)
score = 0
for line in random_lines:
print(line.strip())
answer = input("请输入答案:")
if answer == line.strip().split("=")[1]:
score += 10
print("本次得分为:", score)
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
file = open("d:\\temp\\unit9\\score.txt", "w")
for line in lines:
if line.startswith(username + ":"):
file.write(username + ":" + str(score) + "\n")
else:
file.write(line)
file.close()
elif choice == "2":
# 查看本次成绩
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
for line in lines:
if line.startswith(username + ":"):
print("本次成绩为:", line.strip().split(":")[1])
elif choice == "3":
# 查看所有成绩
file = open("d:\\temp\\unit9\\score.txt", "r")
lines = file.readlines()
file.close()
for line in lines:
if line.startswith(username + ":"):
print(line.strip())
elif choice == "4":
# 返回上一级菜单
break
else:
print("输入有误,请重新输入。")
break
else:
print("用户名或密码错误,请重新登录。")
else:
print("未找到学生信息,请联系老师添加。")
elif choice == "3":
# 退出系统
break
else:
print("输入有误,请重新输入。")
```
该程序实现了一个简单的四则运算练习模拟系统,包括老师用户和学生用户两种角色。老师用户可以管理题目信息和学生信息,并统计成绩;学生用户可以随机抽取题目进行练习,并查看成绩。所有信息都保存在d:\\temp\\unit9文件夹中,其中题目信息保存在exercise.txt文件中,学生信息保存在score.txt文件中。
阅读全文