【发布时间】:2022-11-03 21:13:36
【问题描述】:
我有这个菜单驱动的 python 程序,它允许用户创建、添加和查看学生记录。该程序将信息存储在一个数组中。
MENU = """\n ---------------- MENU ---------------- \n
Enter [1] to Create
[2] to View """
def main():
while True:
print(MENU)
command = acceptCommand()
runCommand(command)
def acceptCommand():
while True:
command = input("\n Enter choice: ")
if not command in COMMANDS:
print("\n ERROR! Command is not recognized.")
else:
return command
break
def runCommand(command):
if command == '1':
sleep(1)
clear()
create()
elif command == '2':
sleep(1)
clear()
view()
studentRecord = []
def create():
print("\n \t ------- CREATE STUDENT INFORMATION ------- \n")
recordSize = int (input("\n Enter Number of Students: "))
for size in range(recordSize):
studentNumber = size + 1
print("\n -------------------------------------------------")
print("\n Student No.", studentNumber)
studentName = input(" Student Name: ")
math = int(input("\n \t \t Math Grade: "))
science = int(input("\t \t Science Grade: "))
english = int(input("\t \t English Grade: "))
filipino = int(input("\t \t Filipino Grade: "))
average = float((math + science + english + filipino)/4)
average = round(average, 2)
print("\n \t \t Average Grade is:", average)
studentRecord.append([studentNumber, studentName, math, science, english, filipino, average])
print("\n----------------------------------")
print("Student Data created successfully!")
print("----------------------------------")
input("\n \t Press any key to continue...")
clear() # This basically clears the screen and goes to the menu
def view():
print("\n \t ------- VIEW STUDENT RECORD -------")
print("\n -------------------------------------------------")
for student in studentRecord:
print(f" Student No. {student[0]}")
print(f" Student Name: {student[1]}")
print(f"\n \t Math Grade: {student[2]}")
print(f"\t Science Grade: {student[3]}")
print(f"\t English Grade: {student[4]}")
print(f"\t Filipino Grade: {student[5]}")
print(f"\n \t Average: {student[6]}")
print(" -------------------------------------------------")
input("\n Press enter to continue...")
clear() # This basically clears the screen and goes to the menu
所以在这个程序中你可以创建和查看你创建的学生记录,然后你可以再次回到create()函数添加另一组记录。
我的问题是,每当我尝试制作另一组记录时,学号就会再次回到“1”。
例如,我在我的记录中添加了 1 个学生。但不是显示Student No. 3。它再次显示Student No. 1
------- VIEW STUDENT RECORD -------
-------------------------------------------------
Student No. 1
Student Name: Harry
Math Grade: 99
Science Grade: 99
English Grade: 99
Filipino Grade: 99
Average: 99.0
-------------------------------------------------
Student No. 2
Student Name: Luke
Math Grade: 89
Science Grade: 89
English Grade: 89
Filipino Grade: 89
Average: 89.0
-------------------------------------------------
# This is my new added data
Student No. 1
Student Name: Michael
Math Grade: 78
Science Grade: 77
English Grade: 77
Filipino Grade: 77
Average: 77.25
-------------------------------------------------
Press enter to continue...
我尝试删除 studentNumber = size + 1 部分,因为我认为这是我错误的根源。
我尝试将其替换为studentNumber = [x+1 for x in studentRecord[0]],但它会提示错误消息,“IndexError:列表索引超出范围"
有没有办法继续统计最后一个学生号?
【问题讨论】:
-
studentNumber = [x+1 for x in studentRecord[0]]出错,因为第一次通过,studentRecord 没有元素。稍后您将遇到问题,因为您正在创建一个列表
标签: python list multidimensional-array error-handling