我在哪里添加while循环

时间:2019-07-09 10:56:02

标签: python loops while-loop

我制作了词典,您可以在这里查看:-https://photos.app.goo.gl/31onUNN359PXA8wz6

但是我想添加while循环,但是我很困惑我将在哪里添加while循环,这是我的代码。

我想在while循环的哪一行添加

'''python

import json
from difflib import get_close_matches

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

def translate(w):
    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 if yes, or N if no: " % get_close_matches(w, data.keys())[0])
        if yn == "Y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "N":
            return "This Word Dosen't Exist"
        else:
            return "we didn't understand your entry"
    else:
        return "This Word Dosen't Exist Please Double Check Your Word"

word = input("Enter Your Word: ")

output = translate(word)

if type(output) == list:
    for item in output:
        print(item)
else:
    print(output)

'''

我没有收到任何错误,但是我需要在字典中添加while循环,在其中添加while循环对我有帮助!

2 个答案:

答案 0 :(得分:1)

我猜您想循环询问单词,以便它不断发生。 您想要在函数之外使用while循环

while True:
    word = input("Enter Your Word: ")
    output = translate(word)
    if type(output) == list:
        for item in output:
            print(item)
    else:
        print(output)
    again = input("Do you want to look for another word? y/n : ")
    if again.lower() != "y":
        break

答案 1 :(得分:0)

我想您想处理多个查询:

while True:
    word = input("Enter Your Word (or q to quit): ")

    if word == 'q':
        break

    output = translate(word)

    if type(output) == list:
        for item in output:
            print(item)
    else:
        print(output)