Python Forum
GUI calculation mistake
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI calculation mistake
#3
You should not hardcode questions into a test program. The program should be set up to ask any question, and the questions for a particular test should be data, probably read in from a file or retrieved from a database. This is poor example of what I am talking about.
from tkinter import *

answers = []

HISTORY = [
    {   'Q': "In which year did World War II end?",
        'choices': ("A. 1945", "B. 1942", "C. 1980", "D. 2021"),
        'A': "A"},
    {   'Q': "What was the name of the machine used to create German and Japanese codes?",
        'choices': ("A. Axis", "B. Enola", "C. Blitz", "D. Enigma"),
        'A': "D"},
    {   'Q': "Which countries fought on the Eastern Front of World War II?",
        'choices': ("A. Germany/Soviet Union", "B. Britain/Germany", "C. Switzerland/Italy", "D. United States/Japan"),
        'A':"A"},
    {   'Q': "What was the code name for the allied invasion of Normandy?",
        'choices': ("A. VJ Day", "B. VE Day", "C. D-Day",  "D. French Resistance"),
        'A': "C"},
    {   'Q': "In 1945 which two Japanese cities were nuclear bombs detonated?",
        'choices': ("A. Hiroshima/Nagasaki", "B. Tokyo/Hiroshima", "C. Tokyo/Nagasaki"),
        'A': "A"
    }]
 
def quiz(window, title, questions, bg='white', fg='black'):
    window.title(title)
    window.configure(bg=bg)
    Label(window, text=title, bg=bg, fg=fg).pack()

    for question in questions:
        Label(window, text='', bg=bg, fg=fg).pack()
        Label(window, text=question['Q'], bg=bg, fg=fg).pack()

        choices = []
        for choice in question['choices']:
            Label(window, text=choice, bg=bg, fg=fg).pack()
            choices.append(choice[0])
    
        answer = StringVar()
        option = OptionMenu(window, answer, *choices)
        option.configure(fg=fg, bg=bg)
        option.pack()

        answers.append((answer, question))
    
def score_quiz(questions):
    score = 0
    for answer, question in questions:
        if answer.get() == question['A']:
            score += 1
        else:
            choice = ord(question['A']) - ord('A')
            print(f'{question["choices"][choice]} is the answer to {question["Q"]}')
    print('Score =', score)

root = Tk()
quiz(root, 'History', HISTORY, 'red', 'white')
Button(root, text='Check Answers', command=lambda: score_quiz(answers)).pack(pady=10)

root.mainloop()
In this example HISTORY is a list of questions. Each question has a question ('Q'), answer ('A'), and a list of choices ('choices'). The quiz function draws a window to display all the questions and collect the answers. The score_quiz() function checks the answers against the questions and tabulates a score.
Reply


Messages In This Thread
GUI calculation mistake - by rturus - Mar-01-2021, 04:15 PM
RE: GUI calculation mistake - by deanhystad - Mar-01-2021, 05:38 PM
RE: GUI calculation mistake - by deanhystad - Mar-01-2021, 05:54 PM
RE: GUI calculation mistake - by joe_momma - Mar-02-2021, 06:57 PM
RE: GUI calculation mistake - by rturus - Mar-03-2021, 11:39 AM
RE: GUI calculation mistake - by deanhystad - Mar-03-2021, 02:10 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020