Skip to content Skip to sidebar Skip to footer

How Do I Access A Dictionary From A Function To Be Used In Another Function?

For an assignment I have to take a string as an input and write it as a file. Then, a function takes the string from the file and puts each word in a dictionary, with the value bei

Solution 1:

Calling a function doesn't automatically save anything in your current namespace. You have to explicitly assign it.

word_dict = word_freq_dict()

Solution 2:

You can put the return value of the word_freq_dict() into a variable named word_dict, like so:

Instead of

word_freq_dict

, try

word_dict = word_freq_dict()

Solution 3:

Although, as folks have pointed out, it is necessary to save the output of word_freq_dict() into a variable, it isn't sufficient to get your code working. Your next issue is your use of turtle.up(), which from your comment and argument, you don't understand:

t.up() # moves cursor up
t.up(1.5*len(key))

This routine lifts the (virtual) pen off the (virtual) paper so that no line drawing takes place, it doesn't move the cursor nor take an argument. The first call makes sense (just adjust your comment), the second call probably should be a call to forward() instead, after rotating the turtle, e.g. via left(90) to point up the page.

Other issues I see is that you probably want to move the turtle to the center bottom of the page, not (-200, -200), and you probably want to print your text centered to make a proper tower. Finally, you need to sort the results of your dictionary so that the words come out in order of frequency of usage, rather than the default random order. Below, I've addressed these issues and also shown how a defaultdict can be helpful in this situation:

from collections import defaultdict
from turtle import Turtle, Screen

defword_freq_dict(file_name):
    """ Count the amount of times words appear in a string """

    file = open(file_name)
    readFile = file.read()  # read file
    words = readFile.split()  # splits string into words, puts each word as an element in a list
    word_dict = defaultdict(int)  # dictionary for words to be placed in with amount of times they appearfor word in words:
        word_dict[word] += 1# adds item in 'words' to a dictionary and amount of times it appearsreturn word_dict

defword_tower(turtle, screen, file_name):

    word_dict = word_freq_dict(file_name)  # calls dictionary function

    turtle.up()  # lift pen off page
    turtle.goto(0, - screen.window_height() // 2)  # start at the center bottom of the page
    turtle.left(90)  # point turtle up the page for subsequent forward() callsfor key insorted(word_dict, key=lambda k: word_dict[k], reverse=True):  # sort words by frequency
        value = word_dict[key] * 15# probably should compute this value based on # words and page height
        turtle.write(key, font=('Arial', value, 'normal'), align='center')
        turtle.forward(value)

yertle = Turtle()
yertle.hideturtle()  # hide turtle image

screen = Screen()

word_tower(yertle, screen, 'data_file.txt')

screen.exitonclick()

This is not a complete program -- there's more error checking that needs to be done (e.g. when opening the file), decisions need to be made about how to handle mixed case as well as strip punctuation, and other tweaks.

Here's an example of the output when applied to a Mark Twain quote:

enter image description here

(See here for the quote and the context.)

Solution 4:

You need to assign word_dict to the result of word_freq_dict(). You are returning the word_dict in word_freq_dict(), but it is never assigned.

Post a Comment for "How Do I Access A Dictionary From A Function To Be Used In Another Function?"