【问题标题】:I have a problem with my loop that I couldn't describe我的循环有问题,我无法描述
【发布时间】:2020-11-01 11:18:59
【问题描述】:

我是编程新手(已经 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)

【问题讨论】:

  • 请将其减少并增强为预期的MRE。显示中间结果与预期结果的偏差。不接受站外链接。
  • @Prune 好的,我下次会更加小心。这是我第一次问堆栈溢出问题。
  • 明白——我们都是你。只需重复介绍导览,注意“如何提问”和“MRE”部分,并适当更新。
  • 我有空的时候就可以了。顺便说一句,什么介绍导览,因为我不记得了。

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


【解决方案1】:

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

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

【讨论】:

  • 是的,它成功了。 (终于!)非常感谢,我想我还有很多东西要学。
【解决方案2】:

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

在第 50 行,你有

keyword = input("The Word '%s' Does Not Exist! Please Try Again: " % keyword)
break

系统会在这里等待输入,但系统不会对keyword做任何事情。

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

对您的下一个问题的温和建议:最好将您的代码直接放在问题中,而不是让我们在其他网站上查找它。不过不用担心!我希望你喜欢你的编程冒险。

【讨论】:

  • 谢谢,下次我会考虑您的建议。对于我希望我的程序执行的操作,我希望它在第 50 行采用关键字并从顶部重新开始。这样如果第 50 行的关键字是 \end,它将结束,如果它有定义,它将打印定义等等。
猜你喜欢
  • 2016-05-21
  • 2020-04-17
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多