Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'now' is not defined [duplicate]

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

==================================================
like image 707
stevengfowler Avatar asked Dec 02 '25 05:12

stevengfowler


1 Answers

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()).

like image 111
NPE Avatar answered Dec 04 '25 19:12

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!