我需要纠正这个脚本上的错误代码。总共有5个错误。以下是我目前修改的内容。我在第三行定义了一个数组。我一行行地看了一遍,试图改正,但没有成功。我将非常感激在正确的方向上推动得到这个代码的修复。
from array import array
students=array()
def getString(prompt, field):
valid=False
while valid==False:
myString=input(prompt)
if (len(myString)>0):
valid=True
else:
print("The student's " + field + " cannot be empty. Please try again.")
return myString
def getFloat(promp, field):
while True:
try:
fNum=float(getString(prompt, field))
break
except ValueError:
print("That is not a valid number for " + field + ", please try again")
return fNum
def addStudent():
first=getString("Enter the student's first name: ", "first name")
last=getString("Enter the student's last name: ", "last name")
major=getString("Enter the student's major: ", "major")
gpa=getFloat("Enter the student's GPA: ", "GPA")
students.append({"first":first,"last":last,"major":major,"gpa":gpa})
def displayStudents():
print("nCollege Roster:")
print("*************************************************************************")
if (len(students)==0):
print("There are no students to display.")
else:
print("First Name".ljust(20," ")+"Last Name".ljust(30," ")+"Major".ljust(15," ")+"GPA".ljust(6," "))
for i in range(len(students)):
print(students[i]['first'].ljust(20, " "), end="")
print(students[i]['last'].ljust(30, " "), end="")
print(students[i]['major'].ljust(15, " "), end="")
print(str(students[i]['gpa']).ljust(6, " "))
print("*************************************************************************")
def Main():
keepGoing=true
menu="""
*************************************************************************
College Roster System
*************************************************************************
Main Menu:
a) Enter a new Student
b) View all Students
c) Clear Students List
d) Exit
*************************************************************************
Choose an option: """
while keepGoing:
choice=input(menu)
if choice!="":
if choice.lower()=="a":
addStudent()
elif choice.lower()=="b":
displayStudents()
elif choice.lower()=="c":
students.clear()
print("nThe list of students is cleared.")
elif choice.lower()=="d":
keepGoing=False
else:
print("nThat is not a valid selection. Please try again.n")
else:
print("nYour selection cannot be empty. Please try again.n")
print("nOkay, goodbye!!!")
if __name__=="__BC02.py__":
main()
我被困在试图定义数组。我知道还有其他的错误,但是我无法通过这部分。
这是你的代码的一个版本,编译和似乎运行正确:
students=[]
def getString(prompt, field):
valid=False
while valid==False:
myString=input(prompt)
if (len(myString)>0):
valid=True
else:
print("The student's " + field + " cannot be empty. Please try again.")
return myString
def getFloat(prompt, field):
while True:
try:
fNum=float(getString(prompt, field))
break
except ValueError:
print("That is not a valid number for " + field + ", please try again")
return fNum
def addStudent():
first=getString("Enter the student's first name: ", "first name")
last=getString("Enter the student's last name: ", "last name")
major=getString("Enter the student's major: ", "major")
gpa=getFloat("Enter the student's GPA: ", "GPA")
students.append({"first":first,"last":last,"major":major,"gpa":gpa})
def displayStudents():
print("nCollege Roster:")
print("*************************************************************************")
if (len(students)==0):
print("There are no students to display.")
else:
print("First Name".ljust(20," ")+"Last Name".ljust(30," ")+"Major".ljust(15," ")+"GPA".ljust(6," "))
for i in range(len(students)):
print(students[i]['first'].ljust(20, " "), end="")
print(students[i]['last'].ljust(30, " "), end="")
print(students[i]['major'].ljust(15, " "), end="")
print(str(students[i]['gpa']).ljust(6, " "))
print("*************************************************************************")
def main():
keepGoing=True
menu="""
*************************************************************************
College Roster System
*************************************************************************
Main Menu:
a) Enter a new Student
b) View all Students
c) Clear Students List
d) Exit
*************************************************************************
Choose an option: """
while keepGoing:
choice=input(menu)
if choice!="":
if choice.lower()=="a":
addStudent()
elif choice.lower()=="b":
displayStudents()
elif choice.lower()=="c":
students.clear()
print("nThe list of students is cleared.")
elif choice.lower()=="d":
keepGoing=False
else:
print("nThat is not a valid selection. Please try again.n")
else:
print("nYour selection cannot be empty. Please try again.n")
print("nOkay, goodbye!!!")
if __name__=="__main__":
main()
大多数错误是基本的打字错误。对于第3行的第一个错误,我认为最好使用标准的Pythonlist
来存储每个学生的数据。使用[]
创建一个列表。