3

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

==================================================
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
stevengfowler
  • 141
  • 2
  • 3
  • 12
  • 2
    `for i in range(len(strong)):` not sure if you copy/pasted incorrectly or something, but i`m pretty sure you meant len(string) instead –  Mar 03 '13 at 20:46

2 Answers2

13

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

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thank you! Why did it work in the teacher's video? I'm using Python 3.3. He using a different version perhaps? – stevengfowler Mar 03 '13 at 20:47
  • 1
    @stevengfowler: If you were using Python 3, `input()` would work. Since it doesn't, this means that you're (inadvertently?) using Python 2. – NPE Mar 03 '13 at 20:48
  • When I type python --version, I get Python 3.3.0, though. So, I must be using 3.3? – stevengfowler Mar 03 '13 at 20:50
  • 1
    @stevengfowler: I don't have a crystal ball, but the exception in your question could only have come from Python 2.x. – NPE Mar 03 '13 at 20:52
  • 3
    @stevengfowler: you may be confused about which python is being called. Add `import sys` and then `print(sys.version)` to the start of your program-- that'll almost certainly show 2.something. – DSM Mar 03 '13 at 21:00
0

use raw_input() for python2 and input() in python3. in python2, input() is the same as saying eval(raw_input())

if you're running this on the command line, try $python3 file.py instead of $python file.py additionally in this for i in range(len(strong)): I believe strong should say string

but this code can be simplified quite a bit

def num_vowels(string):
    s = s.lower()
    count = 0
    for c in s: # for each character in the string (rather than indexing)
        if c in ('a', 'e', 'i', 'o', 'u'):
            # if the character is in the set of vowels (rather than a bunch
            # of 'or's)
            count += 1
    return count

strng = input("Enter a statement:")
print("The number of vowels is:", num_vowels(strng), ".")

replacing the '+' with ',' means you don't have to explicitly cast the return of the function to a string

if you'd prefer to use python2, change the bottom part to:

strng = raw_input("Enter a statement: ")
print "The number of vowels is:", num_vowels(strng), "."
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174