jeudi 17 septembre 2020

When I break out of a loop in an elif statement, it won't continue on with else statement

When you type a word with a typo, it is supposed to correct it and verify by asking you if you meant that or not. And if you say no it should prompt a warning that would say " The Word ' corrected word ' Not Found " using the else statement that comes after the elif statement. However it directly goes back to prompting "Enter a Word" which is the very beginning of the loop instead of switching to else statement and prompting "The Word 'typo word' Not Found".

I am new so it might not be how it's supposed to work so I'd be happy if you showed me how to do what I want.

import mysql.connector
from difflib import get_close_matches

con = mysql.connector.connect(
    user = "ardit700_student",
    password = "ardit700_student",
    host = "108.167.140.122",
    database = "ardit700_pm1database"
)

cursor = con.cursor()

cursor.execute("select * from Dictionary")

resultsAll = cursor.fetchall()

words = []

for a in resultsAll:
    words.append(a[0])

end = False
while True:
    if end != True:
        keyword = input("Enter a Word or Type '\exit' to Exit: ")

    cursor.execute("select * from Dictionary where Expression = '%s'" % keyword)

    results = cursor.fetchall()

    if keyword.lower() == "\exit":
        break
    elif len(results) > 0:
        resultList = []
        for a in results:
            resultList.append(a[1])
        for a in resultList:
            print("'%s'" % a)
        end = False
    elif len(get_close_matches(keyword, words, 1)) > 0:
        endCorrection = False
        cursor.execute("select * from Dictionary where Expression = '%s'" % get_close_matches(keyword, words, 1)[0])
        resultsCorrection = cursor.fetchall()
        endCorrection = False
        while True:
            if endCorrection != True:
                query = input("Did You Mean '%s'? Type (Y)es or (N)o: " % resultsCorrection[0][0])
            if query.upper() == "Y":
                resultList = []
                for a in resultsCorrection:
                    resultList.append(a[1])
                for a in resultList:
                    print("'%s'" % a)
                end = False
                break
            elif query.upper() == "N":
                end = False
                break
            else:
                query = input("Unrecognized Input! Please Try Again: ")
                endCorrection = True
    else:
        print("The Word '%s' Not Found" % keyword)
        keyword = input("Please Try Again or Type '\exit' to Exit: ")
        end = True

Aucun commentaire:

Enregistrer un commentaire