我对继续陈述有困难

2024-04-25 08:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

import json
import difflib

from difflib import get_close_matches

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

def Translate (w):
    x= 3
    while x != 0:
                w = w.lower()
                if w in data:
                    return data [w]

                elif len(get_close_matches(w, data.keys())) > 0:
                    yn = input ("Did you mean %s Instead? Enter Y for yes, or no press N if not: "% get_close_matches(w, data.keys())[0])

                if yn == "Y":
                    return data[get_close_matches(w, data.keys())[0]]

                elif yn == "N":
                    return "The word doesn't exist. Please check your spelling."

                elif w.upper() in data: #in case user enters words like USA or NATO
                    return data[w.upper()]

                elif w.title() in data: #if user entered "texas" this will check for "Texas" as well.
                    return data[w.title()]
                else:
                    return "The word doesn't exist. Please double check it."

    word = input("Enter word:")
    print(Translate(word))
    x -= 1

这是我试图添加的内容:

if x == 0:
    input(" Would you like to keep searching? Y or N?")
elif yn == "Y":
    continue
    x+=3
elif yn == "N":
    return "Have a nice day."

我正在尝试添加一个while循环以保持搜索,直到用户想要停止为止。
我还是python新手,如果有人能帮助我,请提前谢谢


1条回答
网友
1楼 · 发布于 2024-04-25 08:15:52

你可以用下面的方法来做

while True:
    in_put=input("Enter your word here  or simply press return to stop the program\t")
    if in_put:
        """DO stuff here"""
        print(in_put)
    else:
        break

根据您的任务相应地更改程序

相关问题 更多 >