试图通过用户输入呼叫功能
def monster(name):
with open('yvd.txt') as fd:
input=[x.strip('|').split('|') for x in fd.readlines()]
to_search={x[0]:x for x in input}
print('\n'.join(to_search[name]))
monster_name=input('Input monster to search: ')
monster(monster_name)
好吧,所以我想通过用户输入呼叫功能monster
。 input
要求用户输入要搜索的怪物名称。功能monster
然后搜索输入的怪物并将其打印出来。但是,我怎么才能让这个字符串在函数中被调用呢?输出的试图通过用户输入呼叫功能
实施例:
Input monster to search: Boogie Man
Traceback (most recent call last):
File "C:\Users\Trevor\Desktop\yvd_read.py", line 8, in <module>
monster(monster_name)
TypeError: 'str' object is not callable
>>>
我使用3.3,所以raw_input不再使用 – user1985351 2013-03-24 03:20:54
错误说你正在使用python 2.x你确定你使用python 3.x吗? – diegueus9 2013-03-24 03:22:22
很确定。说到处都是3.3.0。 – user1985351 2013-03-24 03:24:04
您需要替换python内置函数raw_input来读取值而不是输入。请参阅input function here的文档。这里是更新的代码:
def monster(name):
with open('yvd.txt') as fd:
user_input=[x.strip('|').split('|') for x in fd.readlines()]
to_search={x[0]:x for x in input}
print('\n'.join(to_search[name]))
monster_name=raw_input('Input monster to search: ')
monster(monster_name)
我不能。我正在使用3.3,并且raw_input在3.3中不再使用。另一个人想到了同样的事情。 – user1985351 2013-03-24 03:29:41
你是什么意思的“让它有可以在函数中调用字符串”? – yehe 2013-03-24 02:57:40
不太明白你的意思。也许你应该更清楚地描述你的问题。如果你能够提供你的输入和输出样本,那会更好。 – zhangyangyu 2013-03-24 03:01:39
monster_name将会是一个怪物的名字,所以它会是一个字符串,我希望这个名字成为函数怪物的参数。但我收到错误“str object not callable” – user1985351 2013-03-24 03:02:37