From bf2a7f7343b6763552619f7b0c8bc640b6f84254 Mon Sep 17 00:00:00 2001 From: Kevin Lu Date: Tue, 4 Feb 2025 18:44:58 -0800 Subject: [PATCH 1/3] added read me --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..91685e8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +This is a calculator \ No newline at end of file From 27496dc92b2702fa504c657f1d1ca330c1cb8bf0 Mon Sep 17 00:00:00 2001 From: Kevin Lu Date: Tue, 4 Feb 2025 18:45:49 -0800 Subject: [PATCH 2/3] added read me --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91685e8..bd66c52 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -This is a calculator \ No newline at end of file +This is a calculator. \ No newline at end of file From d6ceec0cc0e09cd8c911f036d35d20fcf92a0cee Mon Sep 17 00:00:00 2001 From: Kevin Lu Date: Tue, 4 Feb 2025 18:51:40 -0800 Subject: [PATCH 3/3] added mathematical operations --- Calc.py | 67 ++++++++++++++++++++++--------- README.md | 1 - __pycache__/Calc.cpython-312.pyc | Bin 0 -> 9851 bytes 3 files changed, 49 insertions(+), 19 deletions(-) delete mode 100644 README.md create mode 100644 __pycache__/Calc.cpython-312.pyc diff --git a/Calc.py b/Calc.py index f0fc1ec..4ff4f7d 100644 --- a/Calc.py +++ b/Calc.py @@ -74,14 +74,32 @@ def create_buttons(self): Div = Button(self.master, padx=16, pady=16, bd=4, fg="black", font=('ariel', 20, 'bold'), text="/", bg="grey", command=lambda: self.btn_click("/")) Div.grid(row=5, column=3) + + # Row 5 + # added mod operation + bmod = Button(self.master, padx=16, pady=16, bd=4, fg="black", font=('ariel', 20, 'bold'), text="%", bg="grey", command=lambda: self.btn_click("%")) + bmod.grid(row=6, column=0) - # activity 2: add operations like parenthesis, mod, exponents, and delete below - # note that most other comments you see in this file will be for the challenge + # added left parenthesis operation + blpar = Button(self.master, padx=16, pady=16, bd=4, fg="black", font=('ariel', 20, 'bold'), text="(", bg="grey", command=lambda: self.btn_click("(")) + blpar.grid(row=6, column=1) - # Row 5 - bequal = Button(self.master, padx=16, pady=16, bd=4, width=16, fg="black", font=('ariel', 20, 'bold'), text="=", bg="grey", command=self.answer) - bequal.grid(columnspan=4) - + # added right parenthesis operation + brpar = Button(self.master, padx=16, pady=16, bd=4, fg="black", font=('ariel', 20, 'bold'), text=")", bg="grey", command=lambda: self.btn_click(")")) + brpar.grid(row=6, column=2) + + # added exponents parenthesis operation + bexp = Button(self.master, padx=16, pady=16, bd=4, fg="black", font=('ariel', 20, 'bold'), text="xʸ", bg="grey", command=lambda: self.btn_click("**")) + bexp.grid(row=6, column=3) + + # Row 6 + # added delete operation + bdel = Button(self.master, padx=16, pady=16, bd=4, width=8, fg="black", font=('ariel', 20, 'bold'), text="DEL", bg="grey", command=self.delete) + bdel.grid(row=7, column=0, columnspan=2) + + # reformatted equal button + bequal = Button(self.master, padx=16, pady=16, bd=4, width=8, fg="black", font=('ariel', 20, 'bold'), text="=", bg="grey", command=self.answer) + bequal.grid(row=7, column=2, columnspan=2) def btn_click(self, num): result = self.logic.btn_click(num) @@ -103,29 +121,42 @@ def answer(self): class CalculatorLogic: def __init__(self): self.operator = "" - # add logic to keep track of what to do with saved answer for the challenge + # added logic to keep track of what to do with saved answer + self.reset_next_input = False def btn_click(self, num): - # add logic to keep track of what to do with saved answer for the challenge - # hint: checks to see if we are currently at the saved answer and if the next input is a number and not a operation + # checks to see if we are currently at the saved answer and if the next input is a number and not a operation + if self.reset_next_input and self.reset_next_input and str(num).isdigit(): + # in this case we want to clear it and keep track that we are not at a saved answer anymore + self.operator = "" + self.reset_next_input = False self.operator += str(num) + # not at a saved answer anymore + self.reset_next_input = False return self.operator def clear(self): self.operator = "" return self.operator - # activity 2: create delete function - + # created delete function + def delete(self): + self.operator = self.operator[:-1] + return self.operator + def answer(self): - # add logic to save answers for the challenge - #hint try except to only save valid answers - ans = str(eval(self.operator)) - self.operator = ans - self.operator = "" - return ans - + # try except to only save valid answers + try: + ans = str(eval(self.operator)) + # saving our answer + self.operator = ans + # keep track that we are now at the state with a saved answer + self.reset_next_input = True + return ans + except: + self.operator = "" + return "Error" def main(): diff --git a/README.md b/README.md deleted file mode 100644 index bd66c52..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -This is a calculator. \ No newline at end of file diff --git a/__pycache__/Calc.cpython-312.pyc b/__pycache__/Calc.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1f674903c9ff46db15b1500db4d7c7b23b47d5ba GIT binary patch literal 9851 zcmeHNU2GKB6`tSS+4Zh}F}ATCYyu=pf`3RzNSdG~xP^jonmU2Ru2^Pw#>?9Kmpik7 z-L-I9XzVnRL2YHDMhfZ^$gadAk9n-rmvv2qJIX_)^5QpQM@r?Vo^xk*cE{Nb$!b(7 zQbw9HcV_On=iGC@d+wQg<{!bJpMhid%3ou5n;GUmcvCn8tFUkIE?NHbyOI5ZKm$;1uZWZpa zT;4KV{IXEw(|v^+1(QM#Y*~7+%v-xn=wCzOeeqgo2Zn|Ti9?3JXg$OGT>s(Ahx_{s zAx6|m7%zWBj!wi#I-63!(4M5M=_)ZqJ+3EILyD>Cq^gri!xv4b#^SLoQH{D{bA#zv zJZgvudhreGB%X?$k%{36$5WZCZq!DJD(hNYPPN)~-sYJGSK-DJnbfz~msb5H^4o2;f??t&inMp$mL-TPx9G==)R0VL+Q+SSm z*JL0w%-YV5S>dCaH7PJ(GgtFKYFm-o7N5N@J!L=XSdlsw-z|O8xgvGm-kFy=?@N6T zoA<6Z?}sM*4|neV;{>+#Zz=FVYG0AsKbJZ;K?oBqfE;gdG$|ha6py<74#uh!w^$br zg;h9(SA=D8$r|%%HphHL<<}>KH;KolpoWLC|J~7)byULxLfUrBk{g z=;~$N;36@D8;h;uh~5FPM$?IGGNonY)H-smqn!+q#A6rqkcT{hcL8L)2!iAYAh;%; z0(9vBK>Y%=rjRbI;~mdx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