calculator - Python squarerooting -


i have calculator using tkinter(the entire code program here) squareroot function doesn't work.

  def calculate(self):      """ calculates equasion """      calculation = self.out_box.get("1.0", tk.end)      try:         eval(calculation)      except:         ans = "error"      else:         ans = eval(calculation)       self.clear()      self.out_box.insert(tk.end, ans)    def calc_root(self):      """ calculates equasion root """      import math       self.calculate()      num = self.out_box.get("1.0", tk.end)       try:         math.sqrt(num)      except:         ans = "error"      else:         ans = math.sqrt(num)             self.clear()      self.out_box.insert(tk.end, ans) 

i have button linked calc_root() button. seems no matter number (valid or otherwise) precedes squareroot, returns "error" through except clause.

you need convert types:

num = float(self.out_box.get("1.0", tk.end))  self.out_box.insert(tk.end, str(ans)) 

also try-except-else doesn't make sense:

 try:     math.sqrt(num)  except:     ans = "error"  else:     ans = math.sqrt(num)       

shouldn't be:

 try:     ans = math.sqrt(num)  except:     ans = "error" 

Comments