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
Solution 1:
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()
).
Solution 2:
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), "."
Post a Comment for "NameError: Name 'now' Is Not Defined"