TypeError:无法连接'str'和'builtin_function_or_method'对象

时间:2013-04-26 16:27:59

标签: python

此代码有什么问题?

def welcome(name):
    print "congrats! You created your first Python bank account"+ name


print "Hello welcome to the Python bank Account"
print"To begin please enter your name"
name=raw_input
welcome(name)

3 个答案:

答案 0 :(得分:4)

raw_input()是一个函数,所以你必须调用它才能使它工作,并且它还接受一个可选参数,该参数在被调用时打印:

name=raw_input("To begin please enter your name")

示例:

In [61]: name=raw_input("enter your name")
enter your name foo bar

In [62]: name
Out[62]: ' foo bar'

仅执行name=raw_input只会创建对raw_input的新引用,所以 你实际上是想在你的函数中连接字符串和raw_input welcome引发了错误:

In [63]: name=raw_input

In [64]: name
Out[64]: <function raw_input>

答案 1 :(得分:0)

使用raw_input()代替raw_input

raw_input是一个函数,你必须为它调用函数来返回一个字符串,否则返回一个函数对象。

答案 2 :(得分:0)

尝试:

name = raw_input()

原始输入是一个功能,当你想打电话时你应该使用() 例如:

>>> s = raw_input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

请参阅http://docs.python.org/2/library/functions.html#raw_input

相关问题