mercredi 27 novembre 2019

I'm having trouble understanding a syntax error in Python 3.7

I'm learning Python as a beginner (running Python 3.7 through Spyder), and I'm running into a syntax error that I cannot explain. I've checked it against several examples and against working code, and I still cannot understand what I have wrong in terms of syntax. The error occurs on the line that contains the code - if yn == 'Y':

import json     
import difflib   
from difflib import get_close_matches

content = json.load(open('data.json', 'r'))

def getDefinition(word):  

    word = word.lower()  
    if word in content:
        return content[word]
    elif len(get_close_matches(word, content.keys(), cutoff=0.8)) > 0:
        yn = input('Did you mean %s? Enter Y if yes, N if no.' % (get_close_matches(word, content.keys(), cutoff=0.8)[0])

Syntax error occurs on next line

        if yn == 'Y':
            return get_close_matches(word, content.keys(), cutoff=0.8)[0]
        elif yn == 'N':
            return 'Word does not exist.'
        else:
            return 'Did not understand entry.'
    else:
        return 'Word does not exist.'

word = input('Enter word: ')

output = getDefinition(word)

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

Aucun commentaire:

Enregistrer un commentaire