我的循环出现问题,无法描述

时间:2020-07-12 00:23:07

标签: python python-3.x loops while-loop conditional-statements

我是编程新手(已经1周了),我制作了一个英语词库程序,该程序询问您要定义的单词并弹出该单词的定义,然后循环返回以开始。当询问您要为其定义的单词时,可以键入\ exit并停止该程序。但是,它在程序的后期阶段不起作用。例如:

Enter a Word or Type (\exit) to Exit: rainn 
Did You Mean 'rain'? (y) for YES and (n) for NO: n
The Word 'rainn' Does Not Exist! Please Try Again: \exit

它在这里提示

Did You Mean 'exit'? (y) for YES and (n) for NO: n

它应该在哪里停止了程序。

帮我,我很拼命,尽我所能。

这是程序代码:

import json
from difflib import get_close_matches

data = json.load(open("data.json"))

while True:
keyword = input("Enter a Word or Type (\exit) to Exit: ")
    if keyword == "\exit":
        break
    elif keyword.lower() in data:
        output = data[keyword.lower()]
        if isinstance(output, list):
            for definition in output:
                print("\"%s\"" % definition)
        else:
            print("\"%s\"" % definition)
    elif keyword.title() in data:
        output = data[keyword.title()]
        if isinstance(output, list):
            for definition in output:
                print("\"%s\"" % definition)
        else:
            print("\"%s\"" % definition)
    elif keyword.upper() in data:
        output = data[keyword.upper()]
        if isinstance(output, list):
            for definition in output:
                print("\"%s\"" % definition)
        else:
            print("\"%s\"" % definition)
    elif len(get_close_matches(keyword.lower(), data.keys())) > 0:
        correction = input(
            "Did You Mean %s? (y) for YES and (n) for NO: " % (get_close_matches(keyword.lower(), data.keys())[0]).capitalize())
        while True:
            if correction.lower() == "y":
                output = data[get_close_matches(keyword.lower(), data.keys())[0]]
                if isinstance(output, list):
                    for definition in output:
                        print("\"%s\"" % definition)
                else:
                    print("\"%s\"" % definition)
                break
            elif correction.lower() == "n":
                # giving input "\exit" won't work here where it's supposed to
                keyword = input("The Word '%s' Does Not Exist! Please Try Again or Type (\exit) to Exit: " % keyword)
                break
            else:
                correction = input("Unrecognized Input! Please Try Again: ")
    else:
        # giving input "\exit" won't work here too
        keyword = input("The Word '%s' Does Not Exist! Please Try Again or Type (\exit) to Exit: " % keyword)

2 个答案:

答案 0 :(得分:-1)

这是因为当您在内部while循环中键入'\ exit'并设置关键字变量并且中断时,您随后在外部循环的顶部重新启动并要求输入以再次设置关键字。因此,它将覆盖“ \ exit”。为了解决这个问题,您可以添加一个标志出口来检查是否应该再次要求输入。希望这可以帮助! :)

python3 main.py https://api.github.com/repos/anthonykrivonos/my-repo/git/trees/1234567 .

答案 1 :(得分:-2)

欢迎来到编程世界。可能会令人沮丧,不是吗?

在第50行,您有

./build/examples/openpose/openpose.bin --image_dir examples/media/ + OPTIONS

系统将在这里等待输入,但是系统从不对keyword = input("The Word '%s' Does Not Exist! Please Try Again: " % keyword) break 做任何事情。

我不确定您希望程序执行什么操作。我怀疑您想将keyword更改为input,然后重新开始,以允许用户输入新单词或退出程序。

一个关于下一个问题的温和建议:最好将您的代码放在问题中,而不是让我们去另一个站点查找它。不用担心!希望您喜欢编程之旅。