Question

In: Computer Science

I am working on making a simple grade book that will take user input like name...

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))

Solutions

Expert Solution

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!


Related Solutions

I am working on making a simple gradebook. How it works is it asks user for...
I am working on making a simple gradebook. How it works is it asks user for name and grade. It does this for multiple instances until the person types quit to end the program. The code I have here works. My question is how do I print the output in alphabetical order? Output from the input I put into my program: David 98 Annabelle 87 Josh 91 Ben 95 What I want my code to do (in alphabetical order): Annabelle87...
JAVA - I am asking for the user to input their firstName and lastName but I...
JAVA - I am asking for the user to input their firstName and lastName but I want the method myMethod() to be able to print it when it is called. Is that possible? I'm new to Java and i'm not too sure what I should do to fix this. Also if I were to create a IF statement would I have to declare int amountDeposited; , int accountBalance; , int newBalance; in the new if statement. import java.util.Scanner; import java.util.Arrays;...
C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
I am creating a program in Python that allows the user to input a credit card...
I am creating a program in Python that allows the user to input a credit card number, and determine if the card is valid. This should be done by taking every other number, starting from the right, and adding them together. The doubling each of the other digits, and adding them together as single digits, and then adding the two sums together. For example, if the number 4833 1200 3412 3456 is used, the first sum should be 6+4+2+4+0+2+3+8=29, the...
I am creating a crop watering "simulator" in Python. I have the user input an array...
I am creating a crop watering "simulator" in Python. I have the user input an array and then must compare the placement of water and crops to determine if all the crops in the array are watered. The user will either input a "w" for water or "c" for crop when creating the array. A w cell can water 8 cells around it, including itself. My end result must determine if all the crops will be watered or not. How...
C++ C++ We are prompting input from the users. Users input "I like the book "The...
C++ C++ We are prompting input from the users. Users input "I like the book "The little prince" very much" I want to separate this sentence by the order "I" "like" "the" "book" "the little prince" "very" "much". (Keep the quotation mark of little prince, because it is the way the user entered) and adding this to a arraylist in c++  
I am working on a C++ program, where a user puts in a notation a playing...
I am working on a C++ program, where a user puts in a notation a playing card and the output is the full name of the card.(ex: KH = King of Hearts) I have most of it working but I want to have an error come up when the user puts in the wrong info and then loop back to the original question. Am I setting it up wrong? Pasted below is my code #include<iostream> #include<string> using namespace std; int...
I am working on these questions but I would like someone to review them before I...
I am working on these questions but I would like someone to review them before I submit and add any addition information I may have missed or worse gotten wrong! I provided the questions and then my answers to them. 1) Explain what is blacklisting and whitelisting? 2) iptables: Compare -j DROP vs -j REJECT. Which option would you use to implement a firewall rule that blocks incoming packets and why? 3) State the iptables command you would use to...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
I am working on an accounting project related to a supermarket and would like to seek...
I am working on an accounting project related to a supermarket and would like to seek help with regard to its costing system. I was asked to describe the company’s costing system. I have identified supermarket as a value-added intermediary between suppliers and customers, but how do I classify or describe it's costing system since it does not manufacture its own product. Would there be a manufacturing cost incurred or I can only trace non-manufacturing costs to its products? I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT