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
>>>
答案 0 :(得分:0)
答案 1 :(得分:0)
您需要替换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)