• 1000 Когда вас спросят, для какого слова вы хотите дать определение, вы можете набрать \ exit и остановить программу. Однако это не работает на более поздних этапах программы. Например:
Enter a Word or Type (\exit) to Exit: rainn
Did You Mean 'rain'? (y) for YES and (n) for NO: n
The Word 'rainn' Does Not Exist! Please Try Again: \exit
И здесь запрашивается
Did You Mean 'exit'? (y) for YES and (n) for NO: n
, где следовало остановить программу.
Помогите мне Я в отчаянии Я перепробовал все, что мог .
Вот код программы:
import json
from difflib import get_close_matches
data = json.load(open("data.json"))
while True:
keyword = input("Enter a Word or Type (\exit) to Exit: ")
if keyword == "\exit":
break
elif keyword.lower() in data:
output = data[keyword.lower()]
if isinstance(output, list):
for definition in output:
print("\"%s\"" % definition)
else:
print("\"%s\"" % definition)
elif keyword.title() in data:
output = data[keyword.title()]
if isinstance(output, list):
for definition in output:
print("\"%s\"" % definition)
else:
print("\"%s\"" % definition)
elif keyword.upper() in data:
output = data[keyword.upper()]
if isinstance(output, list):
for definition in output:
print("\"%s\"" % definition)
else:
print("\"%s\"" % definition)
elif len(get_close_matches(keyword.lower(), data.keys())) > 0:
correction = input(
"Did You Mean %s? (y) for YES and (n) for NO: " % (get_close_matches(keyword.lower(), data.keys())[0]).capitalize())
while True:
if correction.lower() == "y":
output = data[get_close_matches(keyword.lower(), data.keys())[0]]
if isinstance(output, list):
for definition in output:
print("\"%s\"" % definition)
else:
print("\"%s\"" % definition)
break
elif correction.lower() == "n":
# giving input "\exit" won't work here where it's supposed to
keyword = input("The Word '%s' Does Not Exist! Please Try Again or Type (\exit) to Exit: " % keyword)
break
else:
correction = input("Unrecognized Input! Please Try Again: ")
else:
# giving input "\exit" won't work here too
keyword = input("The Word '%s' Does Not Exist! Please Try Again or Type (\exit) to Exit: " % keyword)