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):
    Tk.__init__(self)

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

    self.title("Multiplication quiz game")
    self.addTitle()
    self.addStudent()
    self.addOutput()
    self.checkAnswer()
#    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(R_or_W)
    else:
       print ("You are Correct!")
       print(R_or_W)
       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)
   text_file.close()


def main():
  app = App()
  app.mainloop()

if __name__ == "__main__":
  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!

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, networking, learning, and sharing knowledge.