Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple python question

  1. Mar 26, 2010 #1
    Basically, I have a function that draws a house. You enter (x, y) coordinates to pick where the house is drawn. However, it only lets me draw one house. Why can't I draw more then one house by calling the draw_house(x, y) function multiple times? Here's the code:

    Code (Text):
    from gasp import *          # import everything from the gasp library

    begin_graphics()            # open the graphics canvas

    def draw_house(x, y):
        Box((x, y), 100, 100)         # the house
        Box((x+35, y), 30, 50)           # the door
        Box((x+20, y+60), 20, 20)           # the left window
        Box((x+60, y+60), 20, 20)           # the right window
        Line((x, y+100), (x+50, y+140))      # the left roof
        Line((x+50, y+140), (x+100, y+100))     # the right roof

        update_when('key_pressed')  # keep the canvas open until a key is pressed

    draw_house(90, 90)
    draw_house(200, 200)

    end_graphics()              # close the canvas
    In the code above, is draws the first house at (90, 90), but why doesn't it draw the second house?
  2. jcsd
  3. Mar 27, 2010 #2


    Staff: Mentor

    Caveat emptor: I don't know anything about python, so I might be way off in left field here.

    The code starts off by the call to begin_graphics(), which opens the graphics canvas. Then there is the call to draw_house(90, 90), which causes the body of the draw_house function to execute, drawing a house whose opposite corners are at (90, 90) and (190, 190).

    The last line of code in the draw_house function calls update_when, which, when a key is pressed, closes the graphics canvas (which I infer from the comment), so the call to draw_house(200, 200) fails, probably because the graphics canvas is closed.

    That's what I think is going on.
  4. Mar 27, 2010 #3
    Thanks, that was it.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Simple python question Date
Some simple problem with my GUI? Nov 8, 2017
Python Simple python database Sep 19, 2017
Insights Simple Python Debugging with Pdb: Part 2 - Comments Nov 26, 2015
Insights Simple Python Debugging with Pdb - Part 1 - Comments Nov 24, 2015
Python: Running a program - Should be simple Dec 29, 2012