0

I'm sure the answer to this is obvious but I cannot spot it! I have a very basic quiz(using Tkinter and Python 3) which uses 2 arrays , the question is displayed and then the answer entered is matched using the array index when the submit button is clicked.

  1. The question at index 0 is displayed twice- cannot see why
  2. The score does not increment correctly- even though it is a global variable- it just shows 1 each time.
  3. How can I get the quiz to move to a print statement after the end of the list is reached?

I have tried putting an IF statement in the submit function to check the value of i but cannot get this to work. Can anyone point out my errors please?

from tkinter import *

global questions
questions =["What is the name of the Simpsons' next door neighbour?","What is the name of the    school bus driver?",
        "Who runs the Kwik-e-mart?","What does Bart do at the end of the opening credits?"]
global answers
answers = [ "Ned Flanders","Otto","Apu","Write On The Blackboard"]

global score
score = 0
global i
i = 0




def submit():
    '''runs the submit button'''
    global i
    global score

    question.config(text=questions[i])
    if answer.get().lower()==answers[i].lower():
        score+=1
    else:
        score=score
    i+=1

    scoretxt.config(text =str(score))
    answer.delete(0,END)


window = Tk()
window.title("Simpsons Quiz")
window.wm_iconbitmap("homer.ico")
window.configure(background ="#ffd600")

banner = PhotoImage(file ="the-simpsons-banner.gif")
Label(window,image = banner).grid(row = 0,columnspan = 6)
Label(window,text = "Question : ",bg ="#ffd600",justify=LEFT).grid(row = 1,column = 0)
Label(window,text = "Type answer here: ",bg = "#ffd600",justify=LEFT).grid(row = 3, column = 0)
scoreLabel =Label(window,bg = "#ffd600")
scoretxt = Label(window,text ="Your score is: ?",bg = "#ffd600")
scoreLabel.grid(row=5,column = 2)
scoretxt.grid(row = 6,column = 2)
question=Label(window,bg = "white",text= questions[0],justify=LEFT)
question.grid(row =1,column=1)
answer = Entry(window,bg ="white",width = 30)
answer.grid(row = 3,column=1)

# make a submit button

Button(window,text= "Submit",bg = "white",command = submit).grid(row = 3,column = 2)

mainloop()
user2127168
  • 91
  • 2
  • 11

1 Answers1

0

1) You are printing the question before you increment i. That's why you get twice.
2) It is always 1 becuase of your usage of global. In python you use global keyword in which scope you want to change your variable. Sadly, my english is not good enough to explain this. Please check out these answers.
3) You can use try-except block. I used it there because that is the exact line where you get the error. You can expand its range.

from tkinter import *

questions =["What is the name of the Simpsons' next door neighbour?","What is the name of the    school bus driver?",
        "Who runs the Kwik-e-mart?","What does Bart do at the end of the opening credits?"]

answers = [ "Ned Flanders","Otto","Apu","Write On The Blackboard"]

#removed globals from here
score = 0
i = 0

def submit():
    '''runs the submit button'''
    global i
    global score

    if answer.get().lower()==answers[i].lower():
        score+=1

    i+=1 #first increment, then show the question since you already show it at startup

    try:  #since you get the IndexError on this line, I used on here
        question.config(text=questions[i])
    except IndexError:
        print ("something")

    scoretxt.config(text = "Your score is: {}".format(str(score)))
    answer.delete(0,END)


window = Tk()
window.title("Simpsons Quiz")
window.wm_iconbitmap("homer.ico")
window.configure(background ="#ffd600")

banner = PhotoImage(file ="the-simpsons-banner.gif")
Label(window,image = banner).grid(row = 0,columnspan = 6)
Label(window,text = "Question : ",bg ="#ffd600",justify=LEFT).grid(row = 1,column = 0)
Label(window,text = "Type answer here: ",bg = "#ffd600",justify=LEFT).grid(row = 3, column = 0)
scoreLabel =Label(window,bg = "#ffd600")
scoretxt = Label(window,text ="Your score is: ?",bg = "#ffd600")
scoreLabel.grid(row=5,column = 2)
scoretxt.grid(row = 6,column = 2)
question=Label(window,bg = "white",text= questions[0],justify=LEFT)
question.grid(row =1,column=1)
answer = Entry(window,bg ="white",width = 30)
answer.grid(row = 3,column=1)

# make a submit button

Button(window,text= "Submit",bg = "white",command = submit).grid(row = 3,column = 2)

mainloop()

Also you might want to use a dictionary instead of questions-answers lists.

Community
  • 1
  • 1
Lafexlos
  • 7,618
  • 5
  • 38
  • 53