今天我尝试使用布尔表达式(True或False)来完成我的代码;请注意我将命令放在if processor['dictionary'] = True
。当代码存在时,它无法运行。但如果它不存在,它可以正常运行。我无法找到阻止我运行此代码的错误原因,错误是:
IndentationError: expected an indented block
希望你们能帮帮我。
dictionary = {
"chicken":"chicken two leg animal",
"fish":"fish is animal that live under water",
"cow":"cow is big vegetarian animal",
"space monkey":"monkey live in space",
}
print("how can i help you?")
user_input = raw_input()
print
print("You asked: {}.".format(user_input))
processor = {
"dictionary":False,
"dictionary_lookup":[],
}
split = user_input.split(" ")
combos = [' '.join(split[x:y]) for x in range(len(split)) for y in range(len(split)+1) if ' '.join(split[x:y]) != ""]
for w in split:
w = w.lower()
if w in dictionary:
processor["dictionary"] = True
print w
print combos
# if processor ["dictionary"] = True
response = {}
for item in combos:
if dictionary.get(item):
response[item] = "what you were looking for is: {}.".format(dictionary[item])
if not response:
print("Response: I will get back to you!")
print
for ind, (k,v) in enumerate(response.iteritems()):
print("Response {}: {} ({})".format(ind+1, v, k))
print
答案 0 :(得分:1)
Python的布尔值是真/假(带大写字母)。 true不等于True。
可以简单地检查对象的真实性
if processor['kamus']:
# your code here
这与您的代码相同,它更加清晰。如果你没有在if语句中放置比较或其他指令,Python会检查真值(真或假,有数据或为空)
你得到的错误是因为你的缩进是错误的,你需要在语句之后有 4个空格(def,if等)