从这段源代码中:
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)) + ".")
运行时,我遇到了以下错误:
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
==================================================
for i in range(len(strong)):
不确定你是不是复制/粘贴错误了,但我很确定你的意思应该是len(string)
。 - user520288