NameError:未定义名称“现在" [英] NameError: name 'now' is not defined
本文介绍了NameError:未定义名称“现在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从此源代码开始:
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
==================================================
推荐答案
Use raw_input()
instead of input()
.
在Python 2中,后者尝试 eval()
输入,这就是导致异常的原因.
In Python 2, the latter tries to eval()
the input, which is what's causing the exception.
在Python 3中,没有raw_input()
; input()
可以正常工作(不是eval()
).
In Python 3, there is no raw_input()
; input()
would work just fine (it doesn't eval()
).
这篇关于NameError:未定义名称“现在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文