您好,我是一名在代码学院工作的初学者。我在好几个地方问过这个问题,到处搜索,都无法找出问题所在。
目的是验证用户是否已回答问题。这是我的代码
print 'Welcome to the Pig Latin Translator!'
raw_input("Enter a word: ")
original = raw_input
if len(original) > 0:
print original
else:
print "empty"
Run Code Online (Sandbox Code Playgroud)
当我执行代码并输入一个单词时,它会出现以下错误:
Traceback (most recent call last):
File "python", line 5, in <module>
TypeError: object of type 'builtin_function_or_method' has no len()
Run Code Online (Sandbox Code Playgroud)
我已经尝试了代码的许多变体,我不明白发生了什么。我真的很感谢所有的意见。
raw_input 您正在尝试获取函数的长度。您忽略的上一行函数调用的实际结果。
>>> len(raw_input)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'builtin_function_or_method' has no len()
Run Code Online (Sandbox Code Playgroud)
您希望将函数调用的输出存储在original:
original = raw_input("Enter a word: ")
if len(original) > 0:
print original
else:
print "empty"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39250 次 |
| 最近记录: |