我在对continue语句遇到麻烦

问题描述 投票:0回答:1
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!

python python-3.x while-loop
1个回答
0
投票

您可以通过以下方式完成!

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

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

© www.soinside.com 2019 - 2024. All rights reserved.