Python 我的循环有一个问题,我无法';无法描述

Python 我的循环有一个问题,我无法';无法描述,python,python-3.x,loops,while-loop,conditional-statements,Python,Python 3.x,Loops,While Loop,Conditional Statements,我是编程新手(已经1周了),我制作了一个英语同义词表程序,它会问你想要定义的单词,然后弹出该单词的定义,然后循环返回开始。当您被问及要为哪个词定义时,您可以键入\退出并停止程序。然而,它在项目的后期阶段不起作用。例如: 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

我是编程新手(已经1周了),我制作了一个英语同义词表程序,它会问你想要定义的单词,然后弹出该单词的定义,然后循环返回开始。当您被问及要为哪个词定义时,您可以键入\退出并停止程序。然而,它在项目的后期阶段不起作用。例如:

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
它应该停止程序的地方

帮帮我,我绝望了,我尽了我所能

以下是程序的代码:

导入json
从difflib导入获取\u关闭\u匹配
data=json.load(打开(“data.json”))
尽管如此:
关键字=输入(“输入要退出的单词或类型(\exit):)
如果关键字=“\exit”:
打破
数据中的elif关键字.lower()
输出=数据[关键字.lower()]
如果isinstance(输出,列表):
对于输出中的定义:
打印(“\%s\”“%定义)
其他:
打印(“\%s\”“%定义)
数据中的elif关键字.title():
输出=数据[关键字.title()]
如果isinstance(输出,列表):
对于输出中的定义:
打印(“\%s\”“%定义)
其他:
打印(“\%s\”“%定义)
数据中的elif关键字.upper():
输出=数据[关键字.upper()]
如果isinstance(输出,列表):
对于输出中的定义:
打印(“\%s\”“%定义)
其他:
打印(“\%s\”“%定义)
elif len(获取关闭匹配项(关键字.lower(),数据.keys())>0:
校正=输入(
“您的意思是%s吗?(y)表示是,而(n)表示否:”%(get_close_matches(keyword.lower(),data.keys())[0])。大写()
尽管如此:
如果更正.lower()=“y”:
输出=数据[get_close_matches(关键字.lower(),数据.keys())[0]]
如果isinstance(输出,列表):
对于输出中的定义:
打印(“\%s\”“%定义)
其他:
打印(“\%s\”“%定义)
打破
elif correction.lower()=“n”:
#输入“\exit”在这里不起作用
关键字=输入(“单词“%s”不存在!请重试或键入(\exit)退出:“%keyword”)
打破
其他:
更正=输入(“无法识别的输入!请重试:”)
其他:
#在这里输入“\exit”也不起作用
关键字=输入(“单词“%s”不存在!请重试或键入(\exit)退出:“%keyword”)

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

在第50行,您有

keyword = input("The Word '%s' Does Not Exist! Please Try Again: " % keyword)
break
系统将在此处等待输入,但系统从不使用
关键字执行任何操作

我不确定你想要这个程序做什么。我怀疑您想将
输入
更改为
打印
,然后重新开始,允许用户输入新单词或退出程序


对于你的下一个问题,一个温和的建议是:最好将你的代码正确地放在问题中,而不是让我们在另一个网站上找到它。不过别担心!我希望您喜欢您的编程经历。

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


请参见将其降低并提高到预期值。显示中间结果与预期结果的偏差。非网站链接是不可接受的。@Prune好的,下次我会更加小心。这是我第一次问关于堆栈溢出的问题。明白了,我们都是你们。只需重复介绍之旅,注意“如何提问”和“MRE”部分,并适当更新此内容。我有空时,Ok就可以了。顺便说一句,什么介绍旅游,因为我不记得了。谢谢,伙计,下次我会考虑你的建议。对于我希望我的程序做的事情,我希望它在第50行获取关键字,然后从顶部重新开始。因此,如果第50行中的关键字是\end,它将结束,如果它有定义,它将打印定义,等等。是的,它成功了。(最后!)非常感谢,我想我还有很多东西要学。
import json
from difflib import get_close_matches

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

exit = False
while True:
    if exit != True:
        keyword = input("Enter a Word or Type (\exit) to Exit: ")
    if keyword == "\exit" or 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)
        break
    elif keyword.title() in data:
        output = data[keyword.title()]
        if isinstance(output, list):
            for definition in output:
                print("\"%s\"" % definition)
        else:
            print("\"%s\"" % definition)
        break
    elif keyword.upper() in data:
        output = data[keyword.upper()]
        if isinstance(output, list):
            for definition in output:
                print("\"%s\"" % definition)
        else:
            print("\"%s\"" % definition)
        break
    elif len(get_close_matches(keyword, data.keys())) > 0:
        correction = input(
            "Did You Mean %s? (y) for YES and (n) for NO: " % (get_close_matches(keyword, data.keys())[0]))
        while True:
            if correction.lower() == "y":
                output = data[get_close_matches(keyword, 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: " % keyword)
                if keyword == "\exit":
                    exit = True
                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: " % keyword)
        if keyword == "\exit":
            exit = True