我在继续声明时遇到麻烦

时间:2020-01-09 03:22:03

标签: python python-3.x while-loop

fread()

我要添加的内容:

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

我试图添加一个while循环以继续搜索,直到用户想要停止为止。
如果有人可以提前帮助我,我还是不熟悉python!

1 个答案:

答案 0 :(得分: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

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