Python Forum
nameerror: name '--' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nameerror: name '--' is not defined
#1
hi, i was given a code to count the words in a paragraph, but i keep getting "NameError: name 'mobypara' is not defined" every time i run it (mobypara is the name of the paragraph).
i made sure the code and the paragraph are both saved in the same file, but that didn't change anything.

here is the code:
def countWords(filename):
    counts = {}
    inf = open(filename, 'r')
    for line in inf:
        words = line.split()
        for word in words:
            if word in counts:
                counts[word] += 1
            else:
                counts[word] = 1
    return counts
that's my input:

countWords(mobypara.txt)

and that's the output:

Traceback (most recent call last):

File "<ipython-input-3-2217261c2031>", line 1, in <module>
countWords(mobypara.txt)

NameError: name 'mobypara' is not defined

any idea what's wrong?
thanks
nadi
Reply
#2
(Nov-30-2018, 12:25 AM)nadiyaz Wrote:
Error:
File "<ipython-input-3-2217261c2031>", line 1, in <module> countWords(mobypara.txt) NameError: name 'mobypara' is not defined
You need to open the file to read it
https://python-forum.io/Thread-Files
Recommended Tutorials:
Reply
#3
(Nov-30-2018, 12:28 AM)metulburr Wrote:
(Nov-30-2018, 12:25 AM)nadiyaz Wrote:
Error:
File "<ipython-input-3-2217261c2031>", line 1, in <module> countWords(mobypara.txt) NameError: name 'mobypara' is not defined
You need to open the file to read it
https://python-forum.io/Thread-Files

i opened it in line 3 (inf = open(filename, 'r')), do i need to open it before the function to?
Reply
#4
Quote:File "<ipython-input-3-2217261c2031>", line 1, in <module>
countWords(mobypara.txt) <-- No quotes ***********

NameError: name 'mobypara' is not defined
This (mobypara) is a variable name because it does not have quotes around it, and there is no variable named mobypara
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 312 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,913 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,353 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,522 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,935 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,374 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,032 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,404 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,902 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020