Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3.8中使用while循环不需要输出_Python - Fatal编程技术网

在Python3.8中使用while循环不需要输出

在Python3.8中使用while循环不需要输出,python,Python,嘿,我一直在为字典编写代码,但我一直面临一些问题。我是python新手,所以无法理解它。 我使用while循环对其进行迭代,直到没有得到所需的选项,但是如果您输入所需的选项,它不会进入if、else条件,并返回“none”作为输出。但是如果我先输入错误的输出,然后输入正确的输出,它就可以正常工作 这是我的密码: 以下是使用正确输入时得到的输出: 以下是我在第一次使用错误输入时得到的输出: 压痕问题。更正如下: def dict(word): word = word.lower()

嘿,我一直在为字典编写代码,但我一直面临一些问题。我是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 "

如果elif else
应该在
之外,而
则在
之外。您是否进行过任何调试?我不建议使用“dict”作为函数名,因为Python已经使用它作为其内置字典类型的名称。
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.
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 "