Equal Button Errors on Calculator
I am trying to learn how to code and am having issues with my simple calculator code. It works properly but it does not save any info after the "=" button is clicked. So it will do 2*2=6 but then 6*2=error. How can I code in a memory function?
Code:
operator=operator + str(numbers)
Text_Input.set(operator)
def btnClearDisplay():
global operator
operator=""
Text_Input.set("")
def btnEqualsInput():
global operator
sumup=str(eval(operator))
Text_Input.set(sumup)
operator=""**
cal = Tk()
cal. title("Calculator")
operator=""
Text_Input =StringVar()
textdisplay = Entry(cal,font=('arial', 20,'bold'), textvariable=Text_Input, bd=30, insertwidth=4,
bg="pink" , justify='right'). grid(columnspan=4)
btn7=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="7",bg="lavender",command=lambda:btnClick(7)).grid(row=1,column=0)
btn8=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="8", bg="lavender", command=lambda:btnClick(8)).grid(row=1,column=1)
btn9=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="9", bg="lavender", command=lambda:btnClick(9)).grid(row=1,column=2)
Addition=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="+", bg="lavender",command=lambda:btnClick("+")).grid(row=1,column=3)
#=============================================================
btn4=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="4", bg="lavender",command=lambda:btnClick(4)).grid(row=2,column=0)
btn5=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="5", bg="lavender",command=lambda:btnClick(5)).grid(row=2,column=1)
btn6=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="6", bg="lavender",command=lambda:btnClick(6)).grid(row=2,column=2)
Subtraction=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="-", bg="lavender",command=lambda:btnClick("-")).grid(row=2,column=3)
#=============================================================
btn1=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="1", bg="lavender",command=lambda:btnClick(1)).grid(row=3,column=0)
btn2=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="2", bg="lavender",command=lambda:btnClick(2)).grid(row=3,column=1)
btn3=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="3", bg="lavender",command=lambda:btnClick(3)).grid(row=3,column=2)
Multiply=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="*", bg="lavender",command=lambda:btnClick("*")).grid(row=3,column=3)
#=============================================================
btn0=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="0", bg="lavender",command=lambda:btnClick(0)).grid(row=4,column=0)
btnClear=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="C", bg="lavender",command= btnClearDisplay).grid(row=4,column=1)
btnEquals=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="=", bg="lavender", command= btnEqualsInput).grid(row=4,column=2)
Division=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="/", bg="lavender",command=lambda:btnClick("/")).grid(row=4,column=3)
cal.mainloop()
python calculator
New contributor
add a comment |
I am trying to learn how to code and am having issues with my simple calculator code. It works properly but it does not save any info after the "=" button is clicked. So it will do 2*2=6 but then 6*2=error. How can I code in a memory function?
Code:
operator=operator + str(numbers)
Text_Input.set(operator)
def btnClearDisplay():
global operator
operator=""
Text_Input.set("")
def btnEqualsInput():
global operator
sumup=str(eval(operator))
Text_Input.set(sumup)
operator=""**
cal = Tk()
cal. title("Calculator")
operator=""
Text_Input =StringVar()
textdisplay = Entry(cal,font=('arial', 20,'bold'), textvariable=Text_Input, bd=30, insertwidth=4,
bg="pink" , justify='right'). grid(columnspan=4)
btn7=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="7",bg="lavender",command=lambda:btnClick(7)).grid(row=1,column=0)
btn8=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="8", bg="lavender", command=lambda:btnClick(8)).grid(row=1,column=1)
btn9=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="9", bg="lavender", command=lambda:btnClick(9)).grid(row=1,column=2)
Addition=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="+", bg="lavender",command=lambda:btnClick("+")).grid(row=1,column=3)
#=============================================================
btn4=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="4", bg="lavender",command=lambda:btnClick(4)).grid(row=2,column=0)
btn5=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="5", bg="lavender",command=lambda:btnClick(5)).grid(row=2,column=1)
btn6=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="6", bg="lavender",command=lambda:btnClick(6)).grid(row=2,column=2)
Subtraction=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="-", bg="lavender",command=lambda:btnClick("-")).grid(row=2,column=3)
#=============================================================
btn1=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="1", bg="lavender",command=lambda:btnClick(1)).grid(row=3,column=0)
btn2=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="2", bg="lavender",command=lambda:btnClick(2)).grid(row=3,column=1)
btn3=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="3", bg="lavender",command=lambda:btnClick(3)).grid(row=3,column=2)
Multiply=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="*", bg="lavender",command=lambda:btnClick("*")).grid(row=3,column=3)
#=============================================================
btn0=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="0", bg="lavender",command=lambda:btnClick(0)).grid(row=4,column=0)
btnClear=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="C", bg="lavender",command= btnClearDisplay).grid(row=4,column=1)
btnEquals=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="=", bg="lavender", command= btnEqualsInput).grid(row=4,column=2)
Division=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="/", bg="lavender",command=lambda:btnClick("/")).grid(row=4,column=3)
cal.mainloop()
python calculator
New contributor
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago
add a comment |
I am trying to learn how to code and am having issues with my simple calculator code. It works properly but it does not save any info after the "=" button is clicked. So it will do 2*2=6 but then 6*2=error. How can I code in a memory function?
Code:
operator=operator + str(numbers)
Text_Input.set(operator)
def btnClearDisplay():
global operator
operator=""
Text_Input.set("")
def btnEqualsInput():
global operator
sumup=str(eval(operator))
Text_Input.set(sumup)
operator=""**
cal = Tk()
cal. title("Calculator")
operator=""
Text_Input =StringVar()
textdisplay = Entry(cal,font=('arial', 20,'bold'), textvariable=Text_Input, bd=30, insertwidth=4,
bg="pink" , justify='right'). grid(columnspan=4)
btn7=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="7",bg="lavender",command=lambda:btnClick(7)).grid(row=1,column=0)
btn8=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="8", bg="lavender", command=lambda:btnClick(8)).grid(row=1,column=1)
btn9=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="9", bg="lavender", command=lambda:btnClick(9)).grid(row=1,column=2)
Addition=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="+", bg="lavender",command=lambda:btnClick("+")).grid(row=1,column=3)
#=============================================================
btn4=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="4", bg="lavender",command=lambda:btnClick(4)).grid(row=2,column=0)
btn5=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="5", bg="lavender",command=lambda:btnClick(5)).grid(row=2,column=1)
btn6=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="6", bg="lavender",command=lambda:btnClick(6)).grid(row=2,column=2)
Subtraction=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="-", bg="lavender",command=lambda:btnClick("-")).grid(row=2,column=3)
#=============================================================
btn1=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="1", bg="lavender",command=lambda:btnClick(1)).grid(row=3,column=0)
btn2=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="2", bg="lavender",command=lambda:btnClick(2)).grid(row=3,column=1)
btn3=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="3", bg="lavender",command=lambda:btnClick(3)).grid(row=3,column=2)
Multiply=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="*", bg="lavender",command=lambda:btnClick("*")).grid(row=3,column=3)
#=============================================================
btn0=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="0", bg="lavender",command=lambda:btnClick(0)).grid(row=4,column=0)
btnClear=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="C", bg="lavender",command= btnClearDisplay).grid(row=4,column=1)
btnEquals=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="=", bg="lavender", command= btnEqualsInput).grid(row=4,column=2)
Division=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="/", bg="lavender",command=lambda:btnClick("/")).grid(row=4,column=3)
cal.mainloop()
python calculator
New contributor
I am trying to learn how to code and am having issues with my simple calculator code. It works properly but it does not save any info after the "=" button is clicked. So it will do 2*2=6 but then 6*2=error. How can I code in a memory function?
Code:
operator=operator + str(numbers)
Text_Input.set(operator)
def btnClearDisplay():
global operator
operator=""
Text_Input.set("")
def btnEqualsInput():
global operator
sumup=str(eval(operator))
Text_Input.set(sumup)
operator=""**
cal = Tk()
cal. title("Calculator")
operator=""
Text_Input =StringVar()
textdisplay = Entry(cal,font=('arial', 20,'bold'), textvariable=Text_Input, bd=30, insertwidth=4,
bg="pink" , justify='right'). grid(columnspan=4)
btn7=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="7",bg="lavender",command=lambda:btnClick(7)).grid(row=1,column=0)
btn8=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="8", bg="lavender", command=lambda:btnClick(8)).grid(row=1,column=1)
btn9=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="9", bg="lavender", command=lambda:btnClick(9)).grid(row=1,column=2)
Addition=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="+", bg="lavender",command=lambda:btnClick("+")).grid(row=1,column=3)
#=============================================================
btn4=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="4", bg="lavender",command=lambda:btnClick(4)).grid(row=2,column=0)
btn5=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="5", bg="lavender",command=lambda:btnClick(5)).grid(row=2,column=1)
btn6=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="6", bg="lavender",command=lambda:btnClick(6)).grid(row=2,column=2)
Subtraction=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="-", bg="lavender",command=lambda:btnClick("-")).grid(row=2,column=3)
#=============================================================
btn1=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="1", bg="lavender",command=lambda:btnClick(1)).grid(row=3,column=0)
btn2=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="2", bg="lavender",command=lambda:btnClick(2)).grid(row=3,column=1)
btn3=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="3", bg="lavender",command=lambda:btnClick(3)).grid(row=3,column=2)
Multiply=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="*", bg="lavender",command=lambda:btnClick("*")).grid(row=3,column=3)
#=============================================================
btn0=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="0", bg="lavender",command=lambda:btnClick(0)).grid(row=4,column=0)
btnClear=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="C", bg="lavender",command= btnClearDisplay).grid(row=4,column=1)
btnEquals=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="=", bg="lavender", command= btnEqualsInput).grid(row=4,column=2)
Division=Button(cal,padx=16,pady=16,bd=8, fg="black" ,font=('arial', 20,'bold'),
text="/", bg="lavender",command=lambda:btnClick("/")).grid(row=4,column=3)
cal.mainloop()
python calculator
python calculator
New contributor
New contributor
New contributor
asked 38 mins ago
Karina Hernandez
1
1
New contributor
New contributor
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago
add a comment |
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Karina Hernandez is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210642%2fequal-button-errors-on-calculator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Karina Hernandez is a new contributor. Be nice, and check out our Code of Conduct.
Karina Hernandez is a new contributor. Be nice, and check out our Code of Conduct.
Karina Hernandez is a new contributor. Be nice, and check out our Code of Conduct.
Karina Hernandez is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210642%2fequal-button-errors-on-calculator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hey, welcome to Code Review! This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Graipher
33 mins ago