Castellar 0 Newbie Poster

I have following code:

# -*- 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
    correct_answers=0
    false_answers=0
    for lause in sentenceList:
        wordList = re.sub("[^\w]", " ",  lause).split()
        inserted_word=wordList[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()

Everything works fine. I have one problem though, it is in the function get_answers(). When I use my application, it creates text with caps, then i fill in the caps and program should print out how many correct and incorrect answers I had. But it shows it wrong, it just shows info about first cap. For example, I fill in two caps. First answer is correct and second incorrect, program says I had one correct answer and zero incorrect. I am pretty sure that the problem is that for loop in get_answers() runs only once. And that is because program thinks sentenceList is only one sentence. And I have no idea how to fix this. Similar for loop works fine in function new_sentences().Please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.