Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating variables for in-class functions after receiving user feedback

I’m trying to update the variable ‘operation’ from ‘ ‘ to the option the user selects. However, until it loops through, the first question always uses a random operation.

I’ve tried to revise my code as much as possible, and isolate the crucial code. Because of this, you’ll probably need to start the programme more than once to notice that it always starts with a different option despite your selection of another.

import tkinter as ttk1
from tkinter import *
from tkinter import ttk
import random

root = Tk()

def change_frame(frame):
    frame.tkraise()

f1 = Frame(root)
f2 = Frame(root)

f1.grid(row=0, column=0, sticky='NSEW')
f2.grid(row=0, column=0, sticky='NSEW')

option = ""
def select_question(value):
    global option
    option = value

second_frame1 = ttk.LabelFrame(f1, text="How to Play")
second_frame1.grid(row=1, column=0, padx=20, pady=10, sticky="NSEW")

third_frame1 = ttk.LabelFrame(f1, text="Difficulty")
third_frame1.grid(row=3, column=0, padx=20, pady=10, sticky="NSEW")

radio1 = ttk.Radiobutton(third_frame1, text="plus", value="Addition", command=lambda:select_question("+"))
radio1.grid(row=2, column=0, padx=18)

radio2 = ttk.Radiobutton(third_frame1, text="minus", value="Subtraction", command=lambda:select_question("-"))
radio2.grid(row=2, column=1, padx=18)

radio3 = ttk.Radiobutton(third_frame1, text="times", value="Multiplication", command=lambda:select_question("*"))
radio3.grid(row=2, column=2, padx=18)

fourth_frame1 = ttk.LabelFrame(f1, text="Challenge")
fourth_frame1.grid(row=4, column=0, padx=20, pady=10, sticky="NSEW")

button = ttk.Button(fourth_frame1, text="Start", command=lambda:change_frame(f2))
button.grid(row=3, column=2)

top_frame2 = ttk.LabelFrame(f2, text="Question", height=100, width=200)
top_frame2.grid(row=0, column=0, columnspan=10, rowspan=2, padx=20, pady=10, sticky="NSEW")

class Question:
    def __init__(self, root1):
        self.root1 = root1
        self.question_entry()
        self.generate_question()

    def question_entry(self):
        self.question = ttk1.Label(top_frame2, text="")
        self.question.grid(pady=20)  

    def generate_question(self):
        num1 = random.randint(1, 20)
        num2 = random.randint(1, 20)
        operation = option

        if operation == "":
            operators = ['+', '-', '*', '/']
            operation = random.choice(operators)
        if operation == "+":
            self.correct_answer = num1 + num2
        elif operation == "-":
            self.correct_answer = num1 - num2
        elif operation == "*":
            self.correct_answer = num1 * num2
        else:
            num1, num2 = num1 * num2, num1
            self.correct_answer = num1 // num2        
        self.question.config(text=f"What is {num1} {operation} {num2}?")   

app = Question(root)
change_frame(f1)
root.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you call self.generate_question() only once inside Question.__init__(), so selecting difficulty later will not generate the question again.

Simply call app.generate_question() at the end of select_question() function to generate new question based on selection.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading