用python语言编写软件完成以下任务: 1.录入几位学生信息(姓名,学号,饭卡余额),并保存到student.txt文件中; 2.能够依据学号对饭卡充值,每次充值的金额为10的倍数; 3.能够按照学号查询饭卡余额,如果谁的余额少于10元,就给出充值提示。 要求:软件至少应具有“输入初始数据”、“饭卡充值”、“查询余额”、“退出”4项菜单。
时间: 2023-12-25 17:05:09 浏览: 33
好的,以下是用Python编写的实现以上任务的示例代码:
```python
def main():
# 读取学生信息
students = []
with open('student.txt', 'r') as f:
for line in f:
name, id, balance = line.strip().split(',')
students.append({'name': name, 'id': id, 'balance': float(balance)})
# 用户界面菜单
while True:
print('请选择操作:')
print('1. 输入初始数据')
print('2. 饭卡充值')
print('3. 查询余额')
print('4. 退出')
choice = input()
if choice == '1':
# 输入初始数据
students = []
n = int(input('请输入学生人数:'))
for i in range(n):
name = input('请输入第{}位学生的姓名:'.format(i+1))
id = input('请输入第{}位学生的学号:'.format(i+1))
balance = float(input('请输入第{}位学生的饭卡余额:'.format(i+1)))
students.append({'name': name, 'id': id, 'balance': balance})
# 保存学生信息
with open('student.txt', 'w') as f:
for student in students:
f.write('{},{},{}\n'.format(student['name'], student['id'], student['balance']))
elif choice == '2':
# 饭卡充值
id = input('请输入要充值的学生学号:')
for student in students:
if student['id'] == id:
amount = int(input('请输入充值金额(必须是10的倍数):'))
if amount % 10 != 0:
print('充值金额必须是10的倍数!')
break
student['balance'] += amount
print('充值成功!')
break
else:
print('没有找到该学生!')
# 保存学生信息
with open('student.txt', 'w') as f:
for student in students:
f.write('{},{},{}\n'.format(student['name'], student['id'], student['balance']))
elif choice == '3':
# 查询余额
id = input('请输入要查询的学生学号:')
for student in students:
if student['id'] == id:
print('学生姓名:{},饭卡余额:{}'.format(student['name'], student['balance']))
if student['balance'] < 10:
print('饭卡余额不足,请及时充值!')
break
else:
print('没有找到该学生!')
elif choice == '4':
# 退出程序
print('退出程序...')
break
else:
print('请输入正确的操作编号!')
```
你可以根据需要对代码进行修改和优化。