首页 > 解决方案 > 测试类似字符串在一个脚本中有效,但在另一个脚本中无效

问题描述

我正在用 Python 制作一个带有海龟的迷宫程序。它来自一个 JSON 问题列表,然后使用海龟输入框提出问题。但是,无论我做什么,答案总是不正确的,因为当答案一字不差时,difflib 不会找到类似的字符串。这在测试脚本中不是问题。逐字使用完全相同的代码。任何人都可以帮忙吗?

这是相关代码:(不起作用的脚本)

def getQuestion():
    questions = []
    f = open("quizdb-20210910134257.json", encoding="utf-8")
    data = json.load(f)
    data = data["data"]
    for i in data["tossups"]:
        question = i["text"]
        questions.append(question)
    f.close()
    return questions
def getAnswer(): 
    answers = []
    f = open("quizdb-20210910134257.json", encoding="utf-8")
    data = json.load(f)
    data = data["data"]
    for i in data["tossups"]: 
        answer = (i["answer"])
        answer = answer.split('>', maxsplit=1)[0]
        answer = answer.split('<', maxsplit=1)[0]
        answer = answer.split('[', maxsplit=1)[0]
        answer = answer.split('(', maxsplit=1)
        answers.append(answer)
    return answers
# Questions and Answers
answers = getAnswer()
questions = getQuestion()

def ask_question(tt): 
    # Clears any text previously there
    tt.clear()
    tt.color("black")
    order = random.randrange(len(questions))
    question = questions[order]
    #adds a new line every 10th word 
    question = '\n'.join(question[i:i+50] for i in range(0, len(question), 50))
    qanswer = turtle.textinput(question,question)
    valid = difflib.get_close_matches(answers[order], qanswer)
    print(valid)
    print(qanswer)
    print(answers[order])
    if valid != []:
        print("Sucecess")
        tt.clear()
        p.forward(30)
        s.listen()
    else:
        #If the answer is incorrect sends player to  begining  
        tt.clear()
        tt.color("Red")
        tt.write("     Incorrect")
        p.penup()
        p.setpos(63.5,-162.5)
        p.pendown()
        s.listen()
        return

执行以下操作的脚本:

import json
import random
import difflib
import turtle


def getQuestion():
    questions = []
    f = open("quizdb-20210910134257.json", encoding="utf-8")
    data = json.load(f)
    data = data["data"]
    for i in data["tossups"]:
        question = i["text"]
        questions.append(question)
    f.close()
    return questions
def getAnswer(): 
    answers = []
    f = open("quizdb-20210910134257.json", encoding="utf-8")
    data = json.load(f)
    data = data["data"]
    for i in data["tossups"]: 
        answer = (i["answer"])
        answer = answer.split('>', maxsplit=1)[0]
        answer = answer.split('<', maxsplit=1)[0]
        answer = answer.split('[', maxsplit=1)[0]
        answer = answer.split('(', maxsplit=1)[0]
        answer = answer.split('ök', maxsplit=1)[0]
        
        answers.append(answer)
    return answers
# Questions and Answers
answers = getAnswer()
questions = getQuestion()


order = random.randrange(len(questions))
question = questions[order]
question = '\n'.join(question[i:i+50] for i in range(0, len(question), 50))
qanswer = turtle.textinput(question,question)
valid = difflib.get_close_matches(answers[order], [qanswer])
if valid != []:
    print("correct")
else: 
    print("wrong")

任何帮助表示赞赏,谢谢。

标签: pythondifflib

解决方案


推荐阅读