简体   繁体   中英

Python control flow with user input

I'm trying to write a dictionary app, with user input word, return definition. Also, if a typo was made, the program loops through 3 top closest matches of the typed and ask user if that is what they meant. My control flow is not running correctly, since 'y' does not break out of the loop or return definition. I don't get any error message. Can someone tell me how to fix? I also what to be able to return 'no close matches' if none of 3 suggestions was what the user meant.

So there are 3 scenarios in the function: word, non-word with close matches(likely to be a typo), non-words without close matches (likely to be random inputs).It's the second scenario I'm having problem with.

    key=input('Enter the word: ')
    key=key.lower()
    dym=get_close_matches(key,data.keys())

    def find_meaning():
        if key in data:
            return data[key]
        elif len(dym)>0:
            for i in dym:
                choice=input('Did you mean '+i + ' Y/N ')
                if choice=='Y' or 'y':
                    break
                    return data[i]
                elif choice=='N' or 'n':
                    continue

        else:
            return 'Word does not exist!'

    print(find_meaning())

After removing 'break' from the above script, I get this output when I run:

Tokas-MBP:Python_Mega_Course tzhu9$ python3 dictionary.py
Enter the word: rainn
Did you mean rain Y/N n
['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetre
s.', 'To fall from the clouds in drops of water.']

The results I intend to see is that program will ask me 'Did you mean' with the second closest match, not outputting the definition of the word I didn't mean to ask.

Try this :

if choice=='Y' or choice=='y':
        break
        return data[i]
elif choice=='N' or choice=='n':
        continue

and I don't understand why that break is there if you are returning something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM