[英]TypeError: must be str, not builtin_function_or_method
I have a bit of a problem at the moment, I'm quite new to python and to people who code a lot this might seem really easy to answer. 目前,我有一个问题,对于python和很多代码编写人员来说,我是一个新手,这似乎很容易回答。 We've been tasked to do a '99 bottles of beer on the wall' challenge and I wanted to make mine slightly better by asking the user what they'd like in the bottle.
我们的任务是挑战“墙上的99瓶啤酒”,我想通过询问用户他们想要的瓶子来使我的啤酒稍微好一点。 However I keep getting an
error: TypeError: must be str, not builtin_function_or_method
但是我不断收到
error: TypeError: must be str, not builtin_function_or_method
input("What would you like inside your bottle?")
def sing(b, end):
print(b or 'No more', 'bottle'+('s' if b-1 else ''), end)
for i in range(99, 0, -1):
sing(i, 'of'+input+'on the wall,')
sing(i, 'of'+input+',')
print('Take one down, pass it around,')
sing(i-1, 'of'+input+'on the wall.\n')
any help would be much appreciated, thanks :3 任何帮助将不胜感激,谢谢:3
input()
method returns value to be assigned to a variable. input()
方法返回要分配给变量的值。 Compare with the docs . 与文档进行比较 。 Now you are referencing a method not a variable.
现在,您引用的是方法而不是变量。
You need something like 你需要类似的东西
something = input('Say sth. ')
print(something)
input
is a builtin function, so when you do 'of'+input+'on the wall'
, you just try to concatenate a function to a string, which doesn't make a lot of sense ;) input
是一个内置函数,因此当您执行'of'+input+'on the wall'
,您只是尝试将一个函数连接到一个字符串,这没有多大意义;)
(the interpreter is explicitly telling you that) (解释器明确告诉您)
input
returns a string you doesn't assign to a variable, so try user_input = input("What would you like inside the bootle?")
and then using user_input
instead of input
in your code. input
返回的字符串是您未分配给变量的,因此请尝试使用user_input = input("What would you like inside the bootle?")
,然后使用user_input
代替代码中的input
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.