Homework 3: Functions, Loops, Conditions, and more Event-Driven Programming
Programming Problem #1: System Functions and DateTime menu
Filename: menu_driver.py , menu_functions.py
Addi#onal files: menu_flowchart.pdf , for the run_menu() function
For this program, write a one-line driver in menu_driver.py thatcalls the func"on run_menu() from menu_fuctions.py . The rest of your func"ons should also be in menu_functions.py .
For this program, we're going to use two libraries: os and datetime , both of which you should import for this problem. Don't assume you can use these modules in future assignments; it's just for this one problem, to have a bit of fun with Python talking to our computer systems.
Your menu should print a list of options:
P for print the menu,
M to display the full name of the current month,
D to get the current day of the week,
T to get the local #me in 24:00:00 hours/minutes/seconds format, W to get the current working directory, and
Q to quit (you can display this op"on, or not - your choice)
This program should run con#nuously un#l the user inputs "Q".
Any other inputs should be ignored, the user no#fied that it is not a valid op#on and the op#ons re-printed (see example for the print message in sample
output below)
This program should demonstrate good func#on design, where each func#on does only one thing (even if that thing is simple). Sample output:
Here is the official documenta"on for date"me: h!ps://docs.python.org/3/library/date"me.html
And here is the official documenta"on for os: h!ps://docs.python.org/3/library/os.html
Try to use Ctrl-F or Command-F (on a Mac) to search for the func"onality you're looking for. However, these are very complicated libraries!
You may find simplified tutorials for these libraries elsewhere on the web. On Piazza, feel free to share ONLY _your search terms_ with your fellow students, but please for this week let them find the actual webpages on their own.
Programming Problem #2: Event-Driven Programming and Click Capture
Filename to submit: shape_clicker.py Starter code: PositionRecord.py
Starter file: Save the image below using "save image as", and use it as your background.
This part of the homework builds on our shape drawing skills from the lab and add some and event-driven programming.
Event-driven programming is the basis of most of what your computer does, if you think about it: your computer programs mostly just sit around and don't run or change informa"on unless you click something on them. So we're going to start using this incredibly important programming paradigm here. NO FLOWCHART or TEST CASES REQUIRED this #me.
Your program should do the following:
- Use the image above as your background for the turtle screen. Remember, the .png image of the applica#on window is 970x635 pixels
- Draw a square with a side length of 90 star#ng at (0, 0). Use green for your pen color.
- Register for mouse clicks
- When the user clicks anywhere inside the square with their mouse, erase the square. (If they click outside the square, do nothing)
- A$er the square has been erased, if they click anywhere on the canvas, draw the square, with the center at the (x, y) coordinates where they clicked
You can watch a demonstra#on of the expected output of the program below:
Note: You may use the Turtle/pen as a global variable. However, other than any constants you use, no variables other than the turtle should be declared in the global namespace.
Instead of using global variables, we have provided the PositionRecord code for your convenience. Import it into your program and then you can use it as a “posi"on record” that allows you to store and retrieve informa"on about your circle regarding its posi"on and visibility. PositionRecord is implemented using object-oriented programming, so you probably won’t understand the code yet. Don’t worry about that, simply use the service via these func"ons:
# Purpose: record the x position . Parameter: the x coordinate you want to record. Does not # Purpose: record the y position. Parameter: the y coordinate you want to record. Does not
# Purpose: record both the x and y position. Parameter: the x coordinate you want to record and the y coordinate you want to record. Does not return anything
get_position_x() # Purpose: get the x position you've previously recorded. Parameters: none Returns: the x position previously recorded.
get_position_y() # Purpose: get the y position you've previously recorded. Parameters: none Returns: the y position previously recorded.
record_visible( visibility ) # Purpose: to record a Boolean value to indicate that you’ve hidden or displayed your shape on the canvas. This Boolean value is meant to represent whether the shape is currently visible or not. Parameters: A Boolean value. Returns: Nothing
is_shape_visible_now() # Purpose: returns a Boolean value that was previously recorded Parameters: none Returns: the Boolean value previously stored here. This Boolean value is meant to represent whether the shape is currently visible or not.
Before record_visible is called the first time, this variable returned by is_shape_visible_now() is set to True.
Important Notes:
It’s very difficult to test graphics-related func"ons without explicit test tools (screen-grabbers, func"onal test tools, etc.), so your most important job is to ensure your turtle movement and drawing is as consistent as possible.
when you move the turtle, it has a direc#onal heading (N/S/E/W). Take care to set the heading consistently to ensure repeatable drawing of the square
record_position_x( x )
return anything
record_position_y( y )
return anything
record_position(x, y)