[Tutor] conditional execution

Chris “Kwpolska” Warrick kwpolska at gmail.com
Tue Apr 1 18:22:07 CEST 2014


On Tue, Apr 1, 2014 at 6:07 PM, Patti Scott <pscott_74 at yahoo.com> wrote:
> I've been cheating:  comment out the conditional statement and adjust the
> indents. But, how do I make my program run with if __name__ == 'main':
> main() at the end?  I thought I understood the idea to run a module called
> directly but not a module imported.  My program isn't running, though.

The statement is:

if __name__ == '__main__':

You’re missing the double underscores on both sides of __main__.  If
you added them in, this would work.

> Below is the last textbook example (Python Programming, Zelle) I reworked.
> Runs when I comment out the conditional statement.  I am self-studying.
> Python 2.7.3,  Notepad++,  Windows PowerShell.
>
> # calc.pyw
> # a 4-function calculator that uses Python arithmetic
> # illustrates use of objects and lists to build a simple GUI
>
> from graphics import *
> from button import Button
>
> class Calculator:

It’s better to inherit from object (i.e. use `class
Calculator(object):`) in this case.  Saves you a lot of trouble when
you encounter new-style-only things.

>     # this class  implements simple calculator GUI
>
>     def __init__(self):
>         # create window for calculator
>         win = GraphWin('calculator')
>         win.setCoords(0,0,6,7)
>         win.setBackground('slategray')
>         self.win = win
>         # now create the widgets
>         self.__createButtons()
>         self.__createDisplay()
>
>     def __createButtons(self):
>         # create list of buttons
>         # start with all the standard-sized buttons
>         # bSpecs gives center coords and labels of buttons
>         bSpecs = [(2,1,'0'), (3,1,'.'),
>             (1,2,'1'), (2,2,'2'), (3,2,'3'), (4,2,'+'), (5,2,'-'),
>             (1,3,'4'), (2,3,'5'), (3,3,'6'), (4,3,'*'), (5,3,'/'),
>             (1,4,'7'), (2,4,'8'), (3,4,'9'), (4,4,'<-'), (5,4,'C')]
>         self.buttons = []
>         for (cx,cy, label) in bSpecs:
>             self.buttons.append(Button(self.win, Point(cx,cy), .75,.75,
> label))
>         # create the larger equals button
>         self.buttons.append(Button(self.win, Point(4.5,1), 1.75, .75, '='))
>         # activate all buttons
>         for b in self.buttons:
>             b.activate()
>
>     def __createDisplay(self):
>         bg = Rectangle(Point(.5,5.5), Point(5.5, 6.5))
>         bg.setFill('white')
>         bg.draw(self.win)
>         text = Text(Point(3,6), "")
>         text.setFace('courier')
>         text.setStyle('bold')
>         text.setSize(16)
>         text.draw(self.win)
>         self.display = text
>
>     def getButton(self):
>         # waits for button to be clicked and returns label of that button
>         while True:
>             p = self.win.getMouse()
>             for b in self.buttons:
>                 if b.clicked(p):
>                     return b.getLabel()  # method exit
>
>     def processButton(self, key):
>         # updates calculator display for press of this key
>         text = self.display.getText()
>         if key == 'C':
>             self.display.setText("")
>         elif key == "<-":
>             # backspace, slice off the last character
>             self.display.setText(text[:-1])
>         elif key == '=':
>             # evaluate the expression and display result
>             # the try ... except mechanism catches errors in the
>             #    formula being evaluated
>             try:
>                 result = eval(text)
>             except:
>                 result ='ERROR'
>             self.display.setText(str(result))
>         else:
>             # normal key press, append it to end of display
>             self.display.setText(text+key)
>
>     def run(self):
>         # infinite event loop to process button clicks
>         while True:
>             key = self.getButton()
>             self.processButton(key)
>
> #this runs the program
> if __name__ == 'main':
>     #first create a calulator object
>     theCalc = Calculator()
> # now call the calculator's run methond
>     theCalc.run()
>
>
>
> and I get
>
> PS C:\Users\Owner\Py Programs> python calc.py
> PS C:\Users\Owner\Py Programs>
>
>
>
> Thanks
>
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense


More information about the Tutor mailing list