我正在写一个程序,学生被写在一个文件,可以搜索时,输入他们的具体数据。不过,我不确定脚本中的search_record方法是否正确。它有时可以返回正确的结果,但有时当输入不同的数据时,它不起作用。在
TA=教师顾问
这是我目前的计划:def read_record():
in_file = open("student.txt", "r")
stud_num = in_file.readline()
while stud_num != "":
last_name = in_file.readline
first_name = in_file.readline
TA = in_file.readline()
stud_num = stud_num.rstrip("\n")
last_name = last_name.rstrip("\n")
first_name = first_name.rstrip("\n")
TA = TA.rstrip("\n")
print "Student number: ", stud_num
print "Last name: ", last_name
print "First name: ", first_name
print "TA: ", TA
stud_num = in_file.readline()
in_file.close()
def write_record():
num_students = input("Enter number of students: ")
out_file = open("student.txt" , "w")
for counter in range (1, num_students + 1):
stud_num = raw_input("Enter student number: ")
last_name = raw_input("Enter last name: ")
first_name = raw_input("Enter first name: ")
TA = raw_input("Enter TA: ")
out_file.write(stud_num +"\n")
out_file.write(last_name +"\n")
out_file.write(first_name +"\n")
out_file.write(TA + "\n")
out_file.close()
def menu():
print "STUDENT MENU"
print "============="
print "A. Create student file"
print "B. Display student file"
print "C. Add student file"
print "D. Search records"
choice = raw_input("Enter your choice: ")
return choice
def add_record():
out_file = open("student.txt", "a")
another = "Y"
while another == "Y":
stud_num = raw_input("Enter student number: ")
last_name = raw_input("Enter last name: ")
first_name = raw_input("Enter first name: ")
TA = raw_input("Enter TA: ")
out_file.write(stud_num + "\n")
out_file.write(last_name + "\n")
out_file.write(first_name + "\n")
out_file.write(TA + "\n")
another = raw_input("Do you want to enter another student (Y or N): ")
out_file.close()
def search_record():
in_file = open("student.txt", "r")
found = False
print "RECORD MENU"
print "============="
print "A. Search student number"
print "B. Search student last name"
print "C. Search student name"
print "D. Search student TA"
choice = raw_input("Enter your choice: ")
if choice == "A":
search = raw_input("Enter a student number: ")
stud_num = in_file.readline()
while stud_num != "":
last_name = in_file.readline()
first_name = in_file.readline()
TA = in_file.readline()
stud_num = stud_num.rstrip("\n")
last_name = last_name.rstrip("\n")
first_name = first_name.rstrip("\n")
TA = TA.rstrip("\n")
if search == stud_num:
print "Student number: ", stud_num
print "Last name: ", last_name
print "First name: ", first_name
print "TA: ", TA
found = True
stud_num = in_file.readline()
elif choice == "B":
search = raw_input("Enter a student last name: ")
last_name = in_file.readline(2)
while last_name != "":
stud_num = in_file.readline()
first_name = in_file.readline()
TA = in_file.readline()
stud_num = stud_num.rstrip("\n")
last_name = last_name.rstrip("\n")
first_name = first_name.rstrip("\n")
TA = TA.rstrip("\n")
if search == last_name:
print "Student number: ", stud_num
print "Last name: ", last_name
print "First name: ", first_name
print "TA: ", TA
found = True
last_name = in_file.readline()
elif choice == "C":
search = raw_input("Enter a student name: ")
first_name = in_file.readline(3)
while first_name != "":
stud_num = in_file.readline()
last_name = in_file.readline()
TA = in_file.readline()
stud_num = stud_num.rstrip("\n")
last_name = last_name.rstrip("\n")
first_name = first_name.rstrip("\n")
TA = TA.rstrip("\n")
first_name = in_file.readline()
if search == first_name:
print "Student number: ", stud_num
print "Last name: ", last_name
print "First name: ", first_name
print "TA: ", TA
found = True
first_name = in_file.readline()
elif choice == "D":
search = raw_input("Enter a TA: ")
TA = in_file.readline(4)
while TA != "":
stud_num = in_file.readline()
last_name = in_file.readline()
first_name = in_file.readline()
stud_num = stud_num.rstrip("\n")
last_name = last_name.rstrip("\n")
first_name = first_name.rstrip("\n")
TA = TA.rstrip("\n")
if search == TA:
print "Student number: ", stud_num
print "Last name: ", last_name
print "First name: ", first_name
print "TA: ", TA
found = True
TA = in_file.readline()
else:
print "Invalid entry"
in_file.close()
if not found:
print "Student record not found"
def main():
choice = menu()
if choice == "A":
write_record()
elif choice == "B":
read_record()
elif choice == "C":
add_record
elif choice == "D":
search_record()
else:
print "error"
main()
我为测试创建的学生文件是
^{pr2}$