Question: Python 3.6 For this function, you'll need to create a test file. The file should have one word on each line, to be used as

Python 3.6

For this function, you'll need to create a test file. The file should have one word on each line, to be used as keys in a dictionary.

Define a function called file_to_rand_dict(). This function is going to create (and return) a dictionary using the words in a file as keys and using random numbers (from 0 up to a some maximum number) as the values. The function should take a string (containing a filename for the keys of the dictionary) for the first argument, and as an optional second argument, it should take an integer (the maximum number for the values* of the dictionary). If the function is called without a second argument, it should use 10 as the maximum value.

Do not try to read the entire file all at once. (So the methods .read() or .readlines() are off limits.) You should be reading through the file one line at a time (using a for loop, or using a while loop with .readline()), and adding to the dictionary as you go.

If there is a problem working with the file (i.e., if an OSError is raised), your function should return an empty dictionary instead of crashing. This function is not meant to communicate with the user, so do not use print() anywhere in this function.

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 Databases Questions!