Python I';我对continue语句有困难
这是我试图添加的内容:Python I';我对continue语句有困难,python,python-3.x,while-loop,Python,Python 3.x,While Loop,这是我试图添加的内容: 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 d
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新手,如果有人能帮助我,请提前谢谢 你可以用以下方法来做
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 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