vendredi 21 mai 2021

Having trouble with with if statements being accessed in python 3

I'm writing a test program that's a math game. I'm working on displaying if the user answers a question correctly that it displays 'correct' (in the shell for now), but it never displays correctly even if all the 10 questions are answered correctly. I have tried multiple things such as changing variables back and forth, which has not achieved anything, and added many print statements to see if the if statement was being accessed, which showed only one thing that was not being accessed.

    from tkinter import *

import random


class GUI:

    def __init__(self,parent):

        self.first_frame=Frame (parent) #The first frame is created here which is one of the two frames on the window this allows for the creation of the second frame which is where information from the rows is printed.
        self.first_frame.grid(row=0,column=0,) #The first frame is grided to the start of where the first window would've been.

        self.count=0

        self.score=0

        global first_frame

        self.starting_window()

    def starting_window (self): #This is where the first screen is set up with the teacher and student option modes
        
        self.starting_frame=Frame(self.first_frame)
        self.starting_frame.grid(row=0,column=0)

        self.student_btn=Button (self.starting_frame,width=50,height=10,text="Student",command=self.student_add_and_sub_options)
        self.student_btn.grid(row=0, column=5)

        self.teacher_btn= Button (self.starting_frame,width=50,height=10,text="Teacher") #,command=
        self.teacher_btn.grid (row=0,column=10)

    def student_add_and_sub_options (self): #Allows students to select if they want to additon or subtraction questions

        self.starting_frame.grid_forget () #removes the previous frame from view with the student and teacher buttons

        self.student_opp_frame=Frame(self.first_frame)
        self.student_opp_frame.grid(row=1,column=1)

        self.add_btn=Button (self.student_opp_frame,width=50,height=10,text="Addition 10 questions",command= self.addition_frame_fun)
        self.add_btn.grid (row=3,column=1)

        self.sub_btn=Button (self.student_opp_frame,width=50,height=10,text="Subtraction 10 questions")
        self.sub_btn.grid (row=3,column=2)

    def addition_frame_fun (self): #Creates the frame where students will be answering addition questions with the labels and entry box.
        
        self.student_opp_frame.grid_forget () 

        self.add_frame= Frame (self.first_frame)
        self.add_frame.grid (row=0,column=0)

        self.title_label=Label (self.add_frame,font='bold',text="Addition Questions")
        self.title_label.grid (row=1,column=1,padx=50)

        self.first_q_num_label= Label (self.add_frame,text="",font='bold')
        self.first_q_num_label.grid (row=3,column=2)

        self.add_q_plus_sign= Label (self.add_frame,text="+",font='bold')
        self.add_q_plus_sign.grid (row=3,column=3,padx=5)

        self.second_q_num_label = Label (self.add_frame,font='bold',text="")
        self.second_q_num_label.grid (row=3,column=4,padx=5)
        
        self.add_ans_entry=Entry (self.add_frame)
        self.add_ans_entry.grid (row=3,column=5,padx=5)

        next_btn=Button (self.add_frame,text="Next Question",command=self.generate_add_question)
        next_btn.grid (row=12,column=5,sticky=SE)

    def generate_add_question (self):
        
        self.count+=1

        if self.count <11:

            self.num1=random.randint (1,10)
            self.num2=random.randint (1,10)

            self.num1_str =str (self.num1)
            self.num2_str=str (self.num2)

            self.total= str(self.num1+self.num2)

            self.first_q_num_label.configure (text=self.num1_str)

            self.second_q_num_label.configure (text=self.num2_str)

            self.check_ans ()

      

    def check_ans (self):
        
        if self.count > 0:

            self.user_add_ans = str (self.add_ans_entry.get () )

           
        if self.user_add_ans == self.total: 
            
            print (self.total)
            print (self.user_add_ans)
            
            print("correct")
            self.score+=1         



    
        
#Main 

first_window = Tk ()
first_window.geometry("700x300")
my_gui= GUI( first_window)

first_window.mainloop ()


Aucun commentaire:

Enregistrer un commentaire