python - 测试类似字符串在一个脚本中有效,但在另一个脚本中无效
问题描述
我正在用 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")
任何帮助表示赞赏,谢谢。
解决方案
推荐阅读
- android - 回收器视图,在整个项目视图上使用点击监听器时不滚动
- c# - C#读取路径没有我把/放在每个子文件夹中
- oracle - 值列表 PL/SQL 函数体在 APEX 中返回 sql 查询而不读取项目值
- r - 将数据框拆分为包含一列常量的列表
- rest - Jitsi REST api:通过认证
- bash - 如何在 bash expect 中返回错误?
- python - VBA - 128位值到128个复选框
- nativescript - Nativescript-Vue 找不到模块'@nstudio/nativescript-mixpanel'
- javascript - IE 11 Script1028 在函数内传播,尝试了 Array.prototype.push.apply
- python-3.x - Python 3 的类型检查工具