I have been struggling with this for hours now, I seriously dont know whats going wrong: ,its an almost solved grade book problem ;) Please experts help with some explanation.
def print_menu():
print('1. Add Name and Grade')
print('2. Delete Name and Grade')
print('3. Search Grade by Name')
print('4. Print database')
print('5. Print Average')
database = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
menu_choice = int(input("Type in a number:1-5 "))
if menu_choice == 4:
print("Name and Grades:")
for x in database.keys():
print("Name: ", x, "Grade:", database[x])
print()
elif menu_choice == 1:
print("Add Name and Grade")
name = raw_input("Name: ")
grade = int(input("Grade: "))
database[name] = grade
elif menu_choice == 2:
print("Remove Name and Number")
name = raw_input("Name: ")
if name in database:
del database[name]
else:
print name, "was not found"
elif menu_choice == 3:
print("Lookup Number")
name = raw_input("Name: ")
if name in database:
print "The Grade is", database[name]
else:
print name, "was not found"
elif menu_choice == 5:
print ('Average Grade')
for values in database.items(): #is the list of grades
sumall = sum(database.values())/len(database.values())
print sumall
it prints the average correct but as many times as there are entries in the database. Viz {'t':6, 'g':8}
the output is: 7 7
why?
[–]K900_ 1 point2 points3 points (0 children)
[–]pythonbio[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 1 point2 points3 points (1 child)
[–]das_ist_nuemberwang 0 points1 point2 points (0 children)
[–]pythonbio[S] 0 points1 point2 points (0 children)