Question: I was asked to write this: Define a function named convert(conversion_dict) that takes one argument, conversion_dict, which is a dictionary that converts numbers (int) to

I was asked to write this:

Define a function named convert(conversion_dict) that takes one argument,  conversion_dict, which is a dictionary that converts numbers (int) to the english word (str) (e.g. {1:'One', 2:'Two'}). This function takes input from the user and returns the number as words (Eg. 1 2 3 -> "One Two Three").
Note: The function should check if the number given is present in the conversion_dict. If not, the function should raise a keyError exception and print "Number not found in conversion dictionary". Similarly, if the user inputs something that can not be converted to integer, then the function should raise a ValueError exception and print "Incorrect Input". In either case, the user should be prompted again to give input. Users should be prompted to give input unless the user types "0".

NOTE: Do not put anything in the input function. Just call input(). If you put anything in, the autograder will be sad


Test Case 1:

convert({1:'One' , 2:'Two' , 3:'Three' , 4:'Four' , 5:'Five'})

Inputs 1 4 9 Three 0

Prints "Number not found in conversion dictionary"

Prints "Incorrect Input"

Returns 'One Four '

The return needs to be a string of the keys that were in the dictionary that the user entered

Here is the program I came up with, it needs to be modified to return a string

def convert(conversion_dict):

    while True:

        try:

            n = int(input("enter input:"))

            if n == 0:

                break

            if n not in conversion_dict.keys():

                raise KeyError

            print(conversion_dict[n])

        except ValueError:

            print("Incorrect Input")

        except KeyError:

            print("Number not found in conversion dictionary")

    return

 

Here is something else I was asked to write this:

Ted has been marking his students' attendance in an excel file (CSV file). He forgot that he needs to put these into the gradebook. He wants to know how many times each student has been absent and present. Define a function ted_gradebook(a_csv_file_name) that takes in a csv file and reads it into a dictionary containing all of the students as keys. Each student key should have a dictionary as its value that has "Absent" and "Present" as keys and each student's tally of days as the values. If anything other than "Present" is marked, the attendance should be counted as Absent.
ted_gradebook(HW 12_1.csv)

returns:

{'Student1': {'Absent': 5, 'Present': 6}, 'Student2': {'Absent': 8, 'Present': 3}, 'Student3': {'Absent': 5, 'Present': 6}, 'Student4': {'Absent': 5, 'Present': 6}, 'Student5': {'Absent': 3, 'Present': 8}, 'Student6': {'Absent': 6, 'Present': 5}, 'Student7': {'Absent': 6, 'Present': 5}, 'Student8': {'Absent': 2, 'Present': 9}, 'Student9': {'Absent': 3, 'Present': 8}, 'Student10': {'Absent': 4, 'Present': 7}, 'Student11': {'Absent': 2, 'Present': 9}, 'Student12': {'Absent': 8, 'Present': 3}, 'Student13': {'Absent': 5, 'Present': 6}, 'Student14': {'Absent': 4, 'Present': 7}, 'Student15': {'Absent': 5, 'Present': 6}, 'Student16': {'Absent': 5, 'Present': 6}, 'Student17': {'Absent': 5, 'Present': 6}, 'Student18': {'Absent': 7, 'Present': 4}, 'Student19': {'Absent': 7, 'Present': 4}, 'Student20': {'Absent': 7, 'Present': 4}, 'Student21': {'Absent': 5, 'Present': 6}, 'Student22': {'Absent': 7, 'Present': 4}, 'Student23': {'Absent': 5, 'Present': 6}, 'Student24': {'Absent': 4, 'Present': 7}, 'Student25': {'Absent': 10, 'Present': 1}, 'Student26': {'Absent': 3, 'Present': 8}, 'Student27': {'Absent': 3, 'Present': 8}, 'Student28': {'Absent': 8, 'Present': 3}, 'Student29': {'Absent': 5, 'Present': 6}, 'Student30': {'Absent': 0, 'Present': 11}, 'Student31': {'Absent': 5, 'Present': 6}, 'Student32': {'Absent': 4, 'Present': 7}, 'Student33': {'Absent': 5, 'Present': 6}, 'Student34': {'Absent': 6, 'Present': 5}, 'Student35': {'Absent': 8, 'Present': 3}}

CSV files is this: (The file continues on to like 35 students this is a shortened version)

,Lab 1,Lab 2,Lab 3,Lab 4,Lab 5,Lab 6,Lab 7,Lab 8,Lab 9,Lab 10,Lab 11

Student1,Present,Present,Present,Present,Absent,Present,Absent,Absent,Absent,Absent,Present

Student2,Absent,Absent,Present,Present,Absent,Absent,Absent,Absent,Present,Absent,Absent

Student3,Absent,Absent,Present,Present,Absent,Present,Present,Absent,Absent,Present,Present

Student4,Absent,Absent,Absent,Absent,Present,Absent,Present,Present,Present,Present,Present

Student5,Present,Present,Present,Absent,Present,Absent,Absent,Present,Present,Present,Present

Student6,Absent,Present,Present,Present,Absent,Absent,Present,Absent,Absent,Present,Absent

Student7,Present,Absent,Present,Present,Present,Absent,Present,Absent,Absent,Absent,Absent

Student8,Present,Present,Absent,Present,Absent,Present,Present,Present,Present,Present,Present

Student9,Absent,Present,Absent,Present,Present,Present,Present,Present,Absent,Present,Present

Student10,Absent,Absent,Present,Absent,Present,Present,Present,Present,Present,Absent,Present

This function needs to read through each line of the text and figure out how many absences and presents a student has. I know the program needs to start off like this:

def ted_gradebook(a_csv_file_name):

    with  open(a_csv_file_name) as fi:

        fi.readline()

        my_file = csv.reader(fi)



Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!