Code error! Ugh. I've worked on this for over 30 minutes and I still can't get it to work. It pulls lines in from a file and saves them to the list. There might be extra empty lines in the file, so it adds those as a slot with just '\n'. I want to delete these slots, and I've tried about 5 different ways. The way shown is going into extra code just to try to make it work. Help, please!
[php]
# imports
import string
# Student Class
class Student(object):
'''Student'''
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
self.schedule = {}
schedule_formatted = ""
self.address = ""
self.city = ""
self.state = ""
self.zip = ""
def __compile_schedule(self):
'''Used for print_schedule()'''
classes_temp = "" # Classes formatted for showing the schedule
period_list = ['1A','2A','3A','4A','5A','1B','2B','3B','4B','5B']
for index in range(5):
period = period_list[index]
classes_temp = classes_temp + "| %-3s | %-33s|\n" % (period,self.schedule['A'][index])
classes_temp = classes_temp + "|" + "-"*40 + "|\n"
for index in range(5):
period = period_list[index+5]
classes_temp = classes_temp + "| %-3s | %-33s|\n" % (period,self.schedule['B'][index])
classes_final = classes_temp[:-1]
return classes_final
def get_schedule(self):
if self.schedule['A']:
schedule_formatted = self.__compile_schedule()
self.schedule_formatted = schedule_formatted
else:
self.per1a = raw_input("Period 1A class: ")
self.per2a = raw_input("Period 2A class: ")
self.per3a = raw_input("Period 3A class: ")
self.per4a = raw_input("Period 4A class: ")
self.per5a = raw_input("Period 5A class: ")
self.per1b = raw_input("Period 1B class: ")
self.per2b = raw_input("Period 2B class: ")
self.per3b = raw_input("Period 3B class: ")
self.per4b = raw_input("Period 4B class: ")
self.per5b = raw_input("Period 5B class: ")
self.schedule = {'1a': self.per1a, '2a': self.per2a, '3a': self.per3a,'4a': self.per4a, '5a': self.per5a, '1b': self.per1b, '2b': self.per2b,'3b': self.per3b,'4b': self.per4b, '5b': self.per5b}
def print_schedule(self):
name = "Name: " + self.name
age = "Age: " + self.age
grade = "Grade: " + self.grade
address = "Address :"
city_state_zip = self.city + ", " + self.state + " " + self.zip
print \
'''
%-23s %s
%-23s %s
%-23s %s
==========================================
| Per | Class |
------------------------------------------
%s
==========================================
''' % (name,address,age,self.address,grade,city_state_zip,self.schedule_formatted)
# Add the address to the person
def add_address(self):
try:
self.address = schedule_data[0][3]
except IndexError:
pass
try:
self.city = schedule_data[0][4]
except IndexError:
pass
try:
self.state = schedule_data[0][5]
except IndexError:
pass
try:
self.zip = schedule_data[0][6]
except IndexError:
pass
# Menu
def menu():
menu = \
'''
(1) - print current person's name
(2) - print current person's schedule
(3) - quit
Choose a selection: '''
choice = 0
choice = raw_input(menu)
while choice < 1 and choice > 3:
choice = raw_input(menu)
if choice == "1":
print currentperson.name
return 0
elif choice == "2":
currentperson.print_schedule()
return 0
elif choice == "3":
print "Thanks for using the Student Scheduling program."
return 1
else:
print "That's not a choice on the menu."
return 0
# Get data from schedules.txt
f = open("schedules.txt","r")
schedule_rawdata = f.readlines()
f.close()
# Split data and put it into schedule_data
schedule_data = []
for line in schedule_rawdata:
schedule_data.append(string.split(line,","))
# strip newlines
for line in schedule_data:
del line[-1]
# Strip empty lines (those with only '\n')
## ERRORS HERE! INDEX ERROR! blah.
for index in range(len(schedule_data)):
## print "index:",index # for debugging
## print "length of index:",len(schedule_data[index]) # for debugging
if len(schedule_data[index]) == 0:
## print temp[index] # for debugging; prints the empty lists
del schedule_data[index] # Error HERE!
# Find number of people
lines = len(schedule_rawdata)
# Declare people from file
num_people = lines/3
person1 = Student(schedule_data[0][0], schedule_data[0][1], schedule_data[0][2])
person1.schedule = {'A':schedule_data[1],'B':schedule_data[2]}
person1.add_address()
# Put people in peoplelist
personlist = []
personlist.append(person1)
currentperson = person1
#Retrieve schedules for people
for person in personlist:
person.get_schedule()
# Run menu until user exits
finished = 0
while finished == 0:
finished = menu()
print schedule_data # why aren't the empty nested lists gone!
[/php]
It still uses the same text file:
Mouche,18,12,1111 SW 1st PL,Dayton,OH,45390,
Underwater Basket Weaving,Concert Band,Lunch,Release,Release,
PE1,PE2,Team Sports,Lunch,Study Hall,
By the way, I took your advice vegaseat and reorganized my code. Thanks.
Thank you for any help.