I have not taken this course. But from my basic understanding of Python, you’re calling the function calculate_frequencies() passing a variable named file_contents as its argument. What type of variable is this? Use of lower(), split() and isalpha() methods in function definition suggest it should be a string type variable. Was it defined / assigned any value earlier? Was any text file opened ant its contents read into the variable?
I’m too getting the same error
And the hints for you are exactly the same as previously - ask yourself (and answer) the questions from my previous post.
In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.
I’m having this problem too! I have observed that the Jupyter Notebook is the culprit:
Hi, @Rushildave . I may be late to the party but here is my shot. I believe you defined a function ‘calculate_frequencies’ which takes ‘file_contents’ as an argument. That is fine. You need to define ‘file_contents’ outside the function if you want to use it anywhere else outside the function (the whole global variable and local variable issue, see e.g @geraldwarp , @Mateusz Kita , @bluesyDan ). Whenever you get the error ‘variable not defined’ within a function call, check its scope-local or global. Currently, file_contents is a variable local to the function and no other outside method can access it. Another method to make variables available outside of the functions that generated them is to ‘return’ them from the function. I have been stuck by overlooking that! Good luck.
Start a new conversation!
Already have an account? Login
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.
Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.
Sorry, our virus scanner detected that this file isn't safe to download.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.
Coursera provides universal access to the world’s best education, partnering with top universities and organizations to offer courses online.
© 2020 Coursera Inc. All rights reserved.