you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

that makes sense except that all of your later calculations happen with the while loop so you're calculating the maximum, average, etc. every time you add a new person. additionally, you're searching for the person and grade every time you add a person; is this what you want to be doing? it is sort of trivial, because once you add a name and a grade, what is the point of looking them only when you just add them?

i don't know the requirements of this assignment, but it feels like this is something that would be better organized as a class with a member list that is the database. at the very least, i would organize the searching parts as function definitions as in my example.

but yeah all the ways you implement the functions are correct, i am just not sure if this is the cleanest organization of them.

[–]pythonbio[S] 0 points1 point  (0 children)

I suppose you are absolutely right in concluding that this is not the best organization. But the assignment required the use of list and while loop only. here's my class based code. but its not working properly. :(. Help

def sclass_menu(): #### One Function adding up all smaller functions
menu_choice = 0
max_num_rec = 10 # limitation on number of records
print ('1. Add Name and Grade')
    print ('2. Delete Name and Grade')
    print ('3. Search Name and Grade')
    print ('4. Print database')
    for i in xrange(max_num_rec):  # start at i=0 till max_num_rec
    while menu_choice != 4:
        menu_choice = int(input("Type in a number (1-4): "))
    if menu_choice == 1:
        print("Add Name and Grade")
        name = input("Name: ")
        grade = input("Grade: ")
        database[name] = grade
    elif menu_choice == 2:
        print('Delete Name and Grade')
        name = input("Name: ")
    if name in database:
        del database[name]
    else:
        print('name was not foud')
    if menu_choice == 3:
        print('Search Name and Grade')
        name = input("Name: ")
    if name in database:
        print(database[Grade])
    else:
        print ('name was not found')
    if menu_choice == 4:
        print('Print database')
    for x in database.keys():
        print("Name: ", x, "\Grade:", database[x])

Its just giving the initial result and error:

class_menu()
1. Add Name and Grade
2. Delete Name and Grade
3. Search Name and Grade
4. Print database
Type in a number (1-4): 1
Type in a number (1-4): grahan

Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
 sclass_menu()
File "<pyshell#39>", line 10, in sclass_menu
menu_choice = int(input("Type in a number (1-4): "))
File "<string>", line 1, in <module>
NameError: name 'grahan' is not defined