This is my python code where I'm trying to create a simple calculator in CodeSkulptor using SimpleGUI functions.
Even though I've defined input1 and input2
I'm ending up with an error:
Line 22: NameError: name 'input2' is not defined
Please help me. Thank you
import simplegui
#initialize globals
def input_handler1(text_input1):
global input1
input1=float(text_input1)
output()
def input_handler2(text_input2):
global input2
input2=float(text_input2)
output()
def button_handler():
print inp1.get_text
def button_handler():
print inp2.get_text
def output():
"""prints contents of input1 and input2"""
print("input1=",input1)
print("input2=",input2)
print (" ")
def swap():
"""swap the contents of input1 and input2"""
global input1,input2
input1,input2=input2,input1
output()
def add():
global input1,input2
input1+=input2
output()
def sub():
global input1,input2
input1-=input2
output()
def mul():
global input1,input2
input1*=input2
output()
def div():
global input1,input2
input1/=input2
output()
frame=simplegui.create_frame("simple_calculator",500,500)
inp1 = frame.add_input('input1', input_handler1, 50)
inp2 = frame.add_input('input2', input_handler2, 50)
frame.add_button("Print",output,100)
frame.add_button("swap",swap,100)
frame.add_button("add",add,100)
frame.add_button("subtract",sub,100)
frame.add_button("multiply",mul,100)
frame.add_button("divide",div,100)
frame.start()
The indentation of docstrings are wrong. And we need to declare
input1andinput2.Here the corrected code (with some other cosmetic change to respect the PEP 8): http://www.codeskulptor.org/#user45_G2dN41wx9eUEWCd.py
PEP 8 -- Style Guide for Python Code