我有以下代码:
# -*- coding: utf-8 -*-
forbiddenWords=['for', 'and', 'nor', 'but', 'or', 'yet', 'so', 'not', 'a', 'the', 'an', 'of', 'in', 'to', 'for', 'with', 'on', 'at', 'from', 'by', 'about', 'as']
def clear_screen():
button2.destroy()
button3.destroy()
text.destroy()
label.destroy()
def main_page():
var = StringVar()
global label
label = Label( root, textvariable=var)
var.set("Fill in the caps: ")
label.pack()
global text
text = Text(root,font=("Purisa",12))
text.pack()
global button
button=Button(root, text ="Create text with caps.", command =lambda: full_function())
button.pack()
def clear_and_main():
clear_screen()
main_page()
def new_sentences(sentenceList):
global replaced_indexes
global replaced_words
replaced_indexes=[]
replaced_words=[]
global newsentences
newsentences=[]
for lause in sentenceList:
import re
from random import randint
s6nade_arv=len(lause.split(' '))
while True:
asendatava_idx=randint(0,s6nade_arv-1)
wordList = re.sub("[^\w]", " ", lause).split()
asendatav_s6na=wordList[asendatava_idx]
if asendatav_s6na.lower() not in forbiddenWords:
replaced_indexes.append(asendatava_idx)
replaced_words.append(asendatav_s6na)
break
uus_lause=lause.replace(asendatav_s6na, "______")
newsentences.append(uus_lause)
return newsentences
def IntoSentences(paragraph):
paragraph = paragraph.decode('utf-8').replace(u'\u014c\u0106\u014d','-')
import nltk.data
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
global sentenceList
sentenceList = sent_detector.tokenize(paragraph.strip())
return sentenceList
def full_function():
global button2
global button3
IntoSentences(text.get(1.0,END))
new_sentences(sentenceList)
text.delete(1.0, END)
button3=Button(root, text ="Main page", command=lambda: clear_and_main())
button3.pack()
button2=Button(root, text ="Answer", command=lambda: get_answers())
button2.pack()
button.destroy()
for i in newsentences:
text.insert(INSERT, str(i))
def get_answers():
IntoSentences(text.get(1.0,END))
i=0
global correct_answers
global false_answers
correct_answers=0
false_answers=0
for lause in sentenceList:
inserted_word=lause[replaced_indexes[i]]
if inserted_word==replaced_words[i]:
correct_answers=correct_answers+1
i=i+1
elif inserted_word!=replaced_words[i]:
false_answers=false_answers+1
i=i+1
text.delete(1.0, END)
text.insert(INSERT, "The number of correct answers: ")
text.insert(END, correct_answers)
text.insert(INSERT, "\n")
text.insert(INSERT, "The number of incorrect answers: ")
text.insert(END, false_answers)
from Tkinter import *
root = Tk()
var = StringVar()
label = Label( root, textvariable=var)
var.set("Fill in the caps: ")
label.pack()
text = Text(root,font=("Purisa",12))
text.pack()
button=Button(root, text ="Create text with caps.", command =lambda: full_function())
button.pack()
root.mainloop()
我的应用程序运行正常,但有一个问题。问题出在函数get_answers()中。当我插入文本并创建带有大写字母的文本并填写大写字母时,我会得到一些文字,告诉我我有多少正确答案以及有多少错误答案。但它总是说它只是最后的答案。例如:我填写两个上限,第一个正确,第二个错误,程序说0正确答案,1个不正确。好像在for循环中定义了correct_answers和false_answers,并且每次函数运行一个句子时都会重新赋值为0。我应该在代码中更改什么才能使程序显示正确答案?
答案 0 :(得分:0)
问题的关键似乎与行
有关inserted_word=lause[replaced_indexes[i]]
其中inserted_word
是单个字符,而不是插入的字。我想你想把字符串标记成单独的单词,但这个功能错过了。