pythonnameerror

NameError: name 'now' is not defined


From this source code:

def numVowels(string):
    string = string.lower()
    count = 0
    for i in range(len(string)):
        if string[i] == "a" or string[i] == "e" or string[i] == "i" or \
            string[i] == "o" or string[i] == "u":
            count += 1
    return count

print ("Enter a statement: ")
strng = input()
print ("The number of vowels is: " + str(numVowels(strng)) + ".")

I am getting the following error when I run it:

Enter a statement:
now

Traceback (most recent call last):
  File "C:\Users\stevengfowler\exercise.py", line 11, in <module>
    strng = input()
  File "<string>", line 1, in <module>
NameError: name 'now' is not defined

==================================================

Solution

  • Use raw_input() instead of input().

    In Python 2, the latter tries to eval() the input, which is what's causing the exception.

    In Python 3, there is no raw_input(); input() would work just fine (it doesn't eval()).