Question

Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...

Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is

MyDict = { 1: 'One', 2: 'Two', 3: 'Three' }

Then ( 2 in MyDict.keys() )

evaluates to True, indicating that 2 is a valid key for MyDict.

Write a Python program that, in a loop, asks the user for a name and then a numerical score, and then asks whether to do another. The user may enter the same name more than one time. When a name is entered, check in the name is already in the dictionaries keys. If it is not (if it's a new name), then add a new pair to the dictionary consisting of the name as the key and the entered score inside a list as the value. If the name is already among the dictionary keys (the name already has one or more scores stored for it), then append the current entered score to the end of the list for that name. When the user is done entering names and scores, create a sorted list of the names. Then, using the sorted list, neatly print out each name and the average score for that name. See the sample output below for an adequate implementation.

C:\Users\brokow\UCM Classes\ME 021\2019 Spring Assignments>HW04_02.py

Enter a name: Jimbo

Enter a score for Jimbo: 15

Enter more data (y/n) ? y

Enter a name: Jolene

Enter a score for Jolene: 21

Enter more data (y/n) ? y

Enter a name: Carlos

Enter a score for Carlos: 19

Enter more data (y/n) ? y

Enter a name: Jolene

Enter a score for Jolene: 22

Enter more data (y/n) ? y

Enter a name: Jimbo

Enter a score for Jimbo: 18

Enter more data (y/n) ? n

Carlos has average score 19.00

Jimbo has average score 16.50

Jolene has average score 21.50

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

Please do give a thumbs up : )

Thank You !
===========================================================================

def main():
    dict ={}
    repeat ='y'
    while repeat=='y':
        name = input('Enter a name: ').title()
        score = int(input('Enter a score for {}: '.format(name.title())))
        if name in dict.keys():
            score_list = dict.get(name)
            score_list.append(score)
        else:
            dict[name]=[score]
        repeat = input('Enter more data (y/n)? ').lower()

    for name in sorted(dict.keys()):
        score_list = dict.get(name)
        total_score = sum(score_list)
        score_count = len(score_list)
        print('{} has average score {:.2f}'.format(name,total_score/score_count))
main()

===========================================================

Code screenshot and output

Please do give a thumbs up : )

Add a comment
Know the answer?
Add Answer to:
Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in python Write a program that keeps a dictionary in which both keys and values are...

    in python Write a program that keeps a dictionary in which both keys and values are strings—names of students and their course grades. Prompt the user of the program to add or remove students, to modify grades, or to print all grades. The printout should be sorted by name and formatted like this: Carl: B- Joe: C Sarah: A Francine: A

  • Write a program that asks for 'name from the user and then asks for a number...

    Write a program that asks for 'name from the user and then asks for a number and stores the two in a dictionary [called the_dicr) as key-value pair. The program then asks if the user wants to enter more data [More data ly/n]?) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list Note: Ignore the case where the name is already...

  • Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

    Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • Your program has a dictionary called classes. The keys in classes are teacher's last names (as...

    Your program has a dictionary called classes. The keys in classes are teacher's last names (as strings), and the corresponding values are the number of students in that teacher's class. For example, if Mr. Brown has 17 students in her class, then 'Brown' might be a key in classes with a corresponding value of 17. Write a few lines in Python that will do the following: Ask the user to enter the last name of a teacher IF that last...

  • they are 2 pictures attached 4) Delete set of keys from Python Dictionary Given: sampleDict =...

    they are 2 pictures attached 4) Delete set of keys from Python Dictionary Given: sampleDict = { "name": "Kelly", "age":25, "salary" : 8000, "city": "New york" } keysToRemove = ["name", "salary"] Expected output: {'city': 'New york', 'age': 25} 1) Write a Python program to iterate over dictionaries using for loops d = {'X': 10, y: 20, 'z': 30) 2) Write a Python program to get the maximum and minimum value in a dictionary my_dict = {'X':500, 'y':5874, 'z': 568} Sample...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are...

    USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are the integers 1 through 10 and whose respective values are the keys cubed (e.g. a key of 5 has a value of 125). b) Write a function that, given a user inputted string, outputs a dictionary where each letter is a key and each value is the letter’s position in the string. Note that you ​must handle cases where a word could contain the...

  • Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries:...

    Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries: - Parameters: file pointer (fp) - Returns : 3 dictionaries of list of tuples - The function displays nothing b) You must use the csv.reader because some fields have commas in them. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing...

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT