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

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 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...
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 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...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
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...
I am working on an Arduino Project. I would like to build a controllable RGB LED...
I am working on an Arduino Project. I would like to build a controllable RGB LED Nightlight (with a debounce code included). With each time pushing the button I want a different color. like the following Initial state = LED off first press= LED turns on Red second Press = LED turns on Green third Press = LED turns on Blue Fourth Press = LED turns on Purple Fifth Press = LED turns on Teal sixth press= LED turns on...
I need assistance in making a simple html and php script that lets a user explor...
I need assistance in making a simple html and php script that lets a user explor the RGB color specturum. It should be able to use 6 buttons (R+,G+,B+,R-,G-,B-) that when pressed, add or subtract 2 points (ranging from 0-255) of that color from the RGB ratio. The new RGB color is then displayed in a small window/box at the bottom of the page for the user to see. This should allow the user to explore all the different colors...
I am making a working reagent that requires a 50:1 ratio (50 of reagent A and...
I am making a working reagent that requires a 50:1 ratio (50 of reagent A and 1 of reagent B) and a total volume of 600uL. Therefore, I need 588uL of reagent A (water) and 12uL of reagent B (lead acetate). I need to make a solution of lead acetate (reagentB) with: 3mM,7.7mM, and 46mM. Upon calculating the mass needed to make 3mM, 7.7mM, and 46mM I got: 0.9756 mg, 2.504 mg, and 14.9592 mg respectively and to each mass...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT