What I am trying to code is a Multiplication quiz game. The trouble I am running into is that I can't seem to figure out how to pass values from one function into another without getting an error.

from Tkinter import *

class App(Tk):
  def __init__(self):

    self.headerFont = ("Helvetica", "16", "bold italic")

    self.title("Multiplication quiz game")
#    self.results()
#    self.saveFile()

  def addTitle(self):

    Label(self, text = "Multiplication Game",
          font = self.headerFont).grid(columnspan = 1)

  def addStudent(self):
    Label(self, text = "Please enter your name: ").grid(row = 1, column =0)
    self.txtName = Entry(self)
    self.txtName.grid(row = 2, column = 0)
    self.txtName.insert(0, "")

    Label(self, text = "Please enter your Student ID Number: ").grid(row = 3, column =0)
    self.txtID = Entry(self)
    self.txtID.grid(row = 4, column = 0)
    self.txtID.insert(0, "")

  def addOutput(self):
    self.btnCalc = Button(self, text = "New Values")
    self.btnCalc.grid(row = 7, columnspan = 1)
    self.btnCalc["command"] = self.calculate

    Label(self, text = "First Value").grid(row = 8, column = 0)
    self.lblFirst_Number = Label(self, bg = "#fff", anchor = "w", relief = "groove")
    self.lblFirst_Number.grid(row =9, column = 0, sticky = "we")

    Label(self, text = "X").grid(row = 10, column = 0)

    Label(self, text = "Second Value").grid(row = 11, column = 0)
    self.lblSecond_Number = Label(self, bg = "#fff", anchor = "w", relief = "groove")
    self.lblSecond_Number.grid(row =12, column = 0, sticky = "we")

    self.btnCalc = Button(self, text = "Check Answer")
    self.btnCalc.grid(row = 15, columnspan = 1)
    self.btnCalc["command"] = self.checkAnswer

    Label(self, text = "Your answer: ").grid(row = 13, column =0)
    self.txtGuess = Entry(self)
    self.txtGuess.grid(row = 14, column = 0)
    self.txtGuess.insert(0, "")

    Label(self, text = "You are...").grid(row = 16, column = 0)
    self.lblR_or_W = Label(self, bg = "#fff", anchor = "w", relief = "groove")
    self.lblR_or_W.grid(row = 17, column = 0, sticky = "we")

  def calculate(self):
    import random
    First_Value= (random.randint(1,10))
    Second_Value= (random.randint(1,10))

    self.lblFirst_Number["text"] = "%.0f" % First_Value
    self.lblSecond_Number["text"] = "%.0f" % Second_Value

  def checkAnswer(self):

    Answer=First_Value*Second_Value    #Problem
    if self.txtGuess!= Answer:
           print ("You are Wrong!")
       print ("You are Correct!")
       self.lblR_or_W["text"] = "%s" % R_or_W

  #def results(self):

  def saveFile(self):

   text_file = open("Student Math Results.txt", "w")
   text_file.write("Student Name: %s"% self.txtName)
   #text_file.write("Student Name: %f"% self.txtID)
   #text_file.write("Student Name: %f"% self.average_1)
   #text_file.write("Student Name: %f"% self.average_2)
   #text_file.write("Student Name: %f"% self.average_3)
   #text_file.write("Student Name: %f"% self.average_4)
   #text_file.write("Student Name: %f"% self.average_5)
   #text_file.write("Student Name: %f"% self.average_6)

def main():
  app = App()

if __name__ == "__main__":

What I can't figure out is how to get the values from the calculate function and pass them into the checkAnswer function. I feel like it should be something easy, but I am just not seeing it. I am not looking for the answer, but if someone could give me an example of what to do so is to point me in the right direction that would be great. Thanks!

Recommended Answers

All 2 Replies

Either make the appropriate variables class variables (by declaring them at the class level) or pass values (arguments other than "self") into the function(s) and return the computed result as a variable.

Take a look at "Function Arguments" and "The Return Statement" Click Here You can also Google for instance objects (also called instance variables) if you want to learn more about classes.

Be a part of the DaniWeb community

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