In: Computer Science
I am working on making a simple grade book that will take user input like name and grade and then return the name and grade of multiple inputs when the person quits the program. Here is the code that I have been working on. This is in Python 3. I can get one line to work but it gives me an error.
Here is what it is supposed to look like:
These are just examples billy 100 greg 60 jane 90 stephanie 70
If I put an name and grade into my code it returns this:
david98
It returns for only one input for each and it finishes with an exit code of 1.
I need to have multiple inputs stored in the dictionary. I am new to dictionaries and I am having a little trouble with it.
Here is the code for my program: grades = {} while True: name = input("Enter a name (or 'quit' to stop): ") if name == 'quit': break grade = input("Enter a grade: ") def set_grade(name, grade): global grades grades[name] = grade def get_grade(name): if name in grades: return grades[name] def get_name(grade): if grade in grades: return grades[grade] set_grade(name, grade) print(name + get_grade(name)) print(grade + get_name(grade))
CODE:
#dictionary to store all the grades
grades = {}
#function to set the name and grade to the dictionary
def set_grade(name, grade):
#the key is the name, and the corresponding value
#is the grade
grades[name] = grade
#function to print names alongside the grade
def print_grades_names():
#grades.keys() returns all the keys
#so we are traversing all the keys in the dictionary
#which are the names stored in the dictionary
for i in grades.keys():
#priting the name and the grade
print('Name: {} Grade: {}'.format(i,str(grades[i])))
#loop
while True:
#asking the name
name = input("Enter a name (or 'quit' to stop): ")
if name == 'quit':
break
#asking the grade
grade = input("Enter a grade: ")
#setting the name and the grade
set_grade(name, grade)
#when setting the grades is done we print the dictionary
#data
print_grades_names()
________________________________________________
CODE IMAGES:
______________________________________________________
Feel free to ask any questions in the comments section
Thank You!