Tuesday, 15 September 2015

python 3.3 - How can I incorporate an input limit of 999 in my calculator script? -



python 3.3 - How can I incorporate an input limit of 999 in my calculator script? -

i aware duplicate past thread, when utilize suggestion, says 'unindent not match outer indentation level'. when run script @ school (this school assignment) blank gui appears no buttons, , when run @ home, appears error message above- using python 3.3.2 on each computer. code 999 limit in lines 45-57 , error message appears on first line (45). i'm quite new python , teacher awful, apologies if question sounds quite silly. can have suggestion how work? lot!

from tkinter import * class calculator(frame): def frame(this, side): w = frame(this) w.pack(side=side, expand=yes, fill=both) homecoming w def button(this, root, side, text, command=none): w = button(root, text=text, command=command) w.pack(side=side, expand=yes, fill=both) homecoming w need_clr = false def digit(self, digit): if self.need_clr: self.display.set('') self.need_clr = false self.display.set(self.display.get() + digit) def sign(self): need_clr = false cont = self.display.get() if len(cont) > 0 , cont[0] == '-': self.display.set(cont[1:]) else: self.display.set('-' + cont) def oper(self, op): self.display.set(self.display.get() + ' ' + op + ' ') self.need_clr = false def calc(self): try: self.display.set(eval(self.display.get())) self.need_clr = true except: showerror('operation error', 'illegal operation') self.display.set('') self.need_clr = false def calc(self): try: self.display.set(self.validate_result(eval(self.display.get()))) self.need_clr = true except: showerror('operation error', 'illegal operation') self.display.set('') self.need_clr = false def validate_result(self, result): if result >= 1000: raise valueerror('result big!') else: homecoming result def __init__(self): frame.__init__(self) self.option_add('*font', 'dotum 15') self.pack(expand=yes, fill=both) self.master.title('simple calculator') self.display = stringvar() e = entry(self, relief=sunken, textvariable=self.display) e.pack(side=top, expand=yes, fill=both) key in ("123", "456", "789"): keyf = self.frame(top) char in key: self.button(keyf, left, char, lambda c=char: self.digit(c)) keyf = self.frame(top) self.button(keyf, left, '0', lambda ch='0': self.digit(ch)) opsf = self.frame(top) char in "+-=": if char == '=': btn = self.button(opsf, left, char, self.calc) else: btn = self.button(opsf, left, char, lambda w=self, s=char: w.oper(s)) clearf = self.frame(bottom) self.button(clearf, left, 'clr', lambda w=self.display: w.set('')) if __name__ == '__main__': calculator().mainloop()

python-3.3

No comments:

Post a Comment