我无法理解Python 3.7中的语法错误

问题描述 投票:0回答:1

[我正在以初学者的方式学习Python(通过Spyder运行Python 3.7),并且遇到了无法解释的语法错误。我已经根据几个示例和有效的代码进行了检查,但我仍然无法理解我在语法方面的错误。错误在包含代码的行上发生-如果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])

下一行出现语法错误

        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)
python-3.x if-statement syntax syntax-error spyder
1个回答
0
投票
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]))
© www.soinside.com 2019 - 2024. All rights reserved.