•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,549 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,544 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 770 | Replies: 3
![]() |
Need help. How do I fix this code?
The instruction from the TUT is this:
Please help. This is the last hard part.
max_points = [25,25,50,25,100]
assignments = ["hw ch 1","hw ch 2","quiz ","hw ch 3","test"]
students = {"#Max":max_points}
def print_menu():
print "1. Add student"
print "2. Remove student"
print "3. Print grades"
print "4. Record grade"
print "5. Save Students"
print "6. Load Students"
print "7. Exit"
def save_students(filename,students):
out_file = open(filename, "w")
for x in students.keys():
out_file.write(x+","+students[x]+"\n")
out_file.close()
def load_students(filename,students):
in_file = open(filename,"r")
while 1:
in_line = in_file.readline()
if len(in_line) == 0:
break
in_line = in_line[-1]
[name,grade]= string.split(in_line,",")
grade[name] = grade
in_file.close()
def print_all_grades():
print "\t",
for i in range(len(assignments)):
print assignments[i],"\t",
print
keys = students.keys()
keys.sort()
for x in keys:
print x,"\t",
grades = students[x]
print_grades(grades)
def print_grades(grades):
for i in range(len(grades)):
print grades[i],"\t\t",
print
print_menu()
menu_choice = 0
while menu_choice != 7:
print
menu_choice = input("Menu Choice (1-6): ")
if menu_choice == 1:
name = raw_input("Student to add: ")
students[name] = [0]*len(max_points)
elif menu_choice == 2:
name = raw_input("Stuent to remove: ")
if students.has_key(name):
del students[name]
else:
print "Student: ",name," not found"
elif menu_choice == 3:
print_all_grades()
elif menu_choice == 4:
print "Record Grade"
name = raw_input("Student: ")
if students.has_key(name):
grades = students[name]
print "Type the number of the grade to record"
print "Type a 0 (zero) to exit"
for i in range(len(assignments)):
print i+1," ",assignments[i],"\t",
print
print_grades(grades)
which = 1234
while which != -1:
which = input("Change which Grade")
which = which-1
if 0 <= which < len(grades):
grade = input("Grade: ")
grades[which] = grade
elif which != -1:
print "Invalid Grade Number"
else:
print "Student not found"
elif menu_choice == 5:
filename = raw_input("Filename to save? ")
save_students(filename,students)
elif menu_choice == 6:
filename = raw_input("Filename to load")
load_students(filename,students)
elif menu_choice != 7:
print_menu()The instruction from the TUT is this:
•
•
•
•
Now modify the grades program from section 11 so that is uses file IO to keep a record of the students.
Please help. This is the last hard part.
•
•
Join Date: Aug 2005
Location: Bosnia and Herzegovina
Posts: 147
Reputation:
Rep Power: 4
Solved Threads: 1
Try to modify your save to file function.
Here's what I would do:
[php]
def save_students(filename,students):
out_file = open(filename, "w")
for x in students.keys():
out_file.write(str(x)+","+str(students[x])+"\n")
out_file.close()
[/php]
Is this is what you want?
#Max.... will be also written to a file.
Here's what I would do:
[php]
def save_students(filename,students):
out_file = open(filename, "w")
for x in students.keys():
out_file.write(str(x)+","+str(students[x])+"\n")
out_file.close()
[/php]
Is this is what you want?
#Max.... will be also written to a file.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Save/Open/Edit (VB.NET)
- Non-programmer tutorial, stuck at list exercise (Python)
- Dell Notebooks NOT Linux Friendly (Getting Started and Choosing a Distro)
- No Connectivity Through Network (Networking Hardware Configuration)
- WinXP Reinstallation (Windows NT / 2000 / XP / 2003)
- windows explore has encountered a problem and needs to close (Windows NT / 2000 / XP / 2003)
Other Threads in the Python Forum
- Previous Thread: Style question
- Next Thread: Non-programmer tutorial, stuck at list exercise



Linear Mode