嘿,我一直在为字典编写代码,但我一直面临一些问题。我是python新手,所以无法理解它。 我使用while循环对其进行迭代,直到没有得到所需的选项,但是如果您输入所需的选项,它不会进入if、else条件,并返回“none”作为输出。但是如果我先输入错误的输出,然后输入正确的输出,它就可以正常工作
这是我的密码:
def dict(word): word = word.lower() if word in data: return data[word] elif len(get_close_matches(word, data.keys())) > 0: yn = input("Did you mean %s instead?Enter Y if yes or N if no >> " % get_close_matches(word,data.keys(), cutoff=0.8)[0]) yn = yn.lower() while not yn in ("yes", "y", "no", "n"): yn = input("Please try Again: ") if yn == "y" or yn == "yes": return data[get_close_matches(word,data.keys(), cutoff=0.8)[0]] elif yn =="n" or yn == "no": return "Word not Found. Please Check if you have typed the right word.\n " else: return "We did not understand your query. Please try Again! " else: return "Word not Found. Please Check if you have typed the right word.\n "
以下是使用正确输入时得到的输出:
Enter Word: rainn Did you mean rain instead?Enter Y if yes or N if no >> Y None
以下是我在第一次使用错误输入时得到的输出:
Enter Word: rainn Did you mean rain instead?Enter Y if yes or N if no >> p Please try Again: y Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres. To fall from the clouds in drops of water.
压痕问题。更正如下:
相关问题 更多 >
编程相关推荐