diff --git a/src/m1e_comments_strings_print.py b/src/m1e_comments_strings_print.py index 18eb8a0..baf39e0 100644 --- a/src/m1e_comments_strings_print.py +++ b/src/m1e_comments_strings_print.py @@ -1,16 +1,33 @@ -######################################################################## +############################################################################### # -# Run this module by right clicking anywhere in this window, then select +# Do this WITH YOUR INSTRUCTOR (live in class): +# +# Read the code below. Predict (in your head) +# what will get printed (i.e., displayed) when the code runs. +# +# Then run this module by right clicking anywhere in this window +# and then selecting: # Run 'name of file' -# After running, find the Console tab (below) and confirm that everything -# did indeed get printed (displayed). This module is just an example. -# Once you have run the file, feel free to play with it some, then you -# can move on to m2. +# (i.e. Run 'm1e_comments_strings...') Or, use the Windows keyboard +# shortcut: Control + Shift + Function-F10. (That is, hold the +# Control, Shift and Function keys down and press the F10 key.) +# +# Confirm that you see the output in the "Run" window that appears. +# Note: I recommend that you click on the "Gear" symbol at the top-right +# part of that window and select +# Move to ~ Right Top +# to make the output easier to see. +# Confirm that the output is what you expected, asking questions as desired. # -######################################################################## +# This module is just an example (m1e, note the e ). +# After you have read and run the code, +# feel free to play with it briefly, then move on to m2. +# +############################################################################### + +print("Hello, World") +print("hi there") +print("one", "two", "buckle my shoe") -print('Hello, World') -print('hi there') -print('one', 'two', 'buckle my shoe') print(3 + 9) -print('3 + 9', 'versus', 3 + 9) +print("3 + 9", "versus", 3 + 9) diff --git a/src/m2_hello_world.py b/src/m2_hello_world.py deleted file mode 100644 index aaa506a..0000000 --- a/src/m2_hello_world.py +++ /dev/null @@ -1,101 +0,0 @@ -print('Hello, World') - -######################################################################## -# This line is a COMMENT -- a note to human readers of this file. -# When a program runs, it ignores everything from a # (hash) mark -# to the end of the line with the # mark. -# -# We call files that have Python code in them MODULES. Line 1 of this -# module (look at it now) prints onto the Console the STRING -# Hello, World -# Anything surrounded by quote marks (single or double) is a STRING. -######################################################################## - -######################################################################## -# -# TODO: 1. -# (Yes, that means for YOU to DO things per these instructions:) -# -# Run this module by right clicking anywhere in this window and select -# Run 'name of file' -# After running, find the Console tab (below) and confirm that -# Hello, World -# did indeed get printed (displayed) on the Console. -# -######################################################################## - -######################################################################## -# -# TODO: 2. -# Notice the small horizontal BLUE bars on the scrollbar-like thing -# on the right. Each blue bar indicates a TO DO in this module. -# -# a. You can use the blue bars to go from one TO DO to the next -# by clicking on the blue bars. ** Try that now. ** -# -# b. When you have completed a TO DO, you should change the word -# TODO -# to -# DONE. -# Try it now on line 16 above, and note that its blue bar on -# the scrollbar-like thing to the right has gone away. -# -# If you change TODOs to DONEs like this, you can tell when you have -# finished all the exercises in a module -- there will be no blue bars -# left on the scrollbar-like thing to the right. -# -# You have now completed TO DO #2, so change its TO DO on line 29 to DONE -# (and proceed similarly for all forthcoming TODOs in this course). -# -######################################################################## - -######################################################################## -# -# TODO: 3. -# Add another print statement below. -# It should print any string that you want (but keep it G-rated!) -# Test your code by re-running this module using either the right click -# method again or by using the play button in the upper right. -# Look at the Console to be sure that your string printed as expected. -# -######################################################################## - -######################################################################## -# -# TODO: 4. -# Add yet another print statement. -# This one should print the *product* of 3,607 and 34,227. -# Let the computer do the arithmetic for you (no calculators!). -# You do NOT have to use strings for this, so no quotation marks! -# -# TEST your code by re-running this module, then asking someone -# whom you trust: What number did your print display for TO DO 4? -# (HINT: It is an INTERESTING number.) Get help if your value is wrong. -# -######################################################################## - -######################################################################## -# -# TODO: 5. -# Look at the list of files in this project to the left. -# Note that this file (m2_hello_world.py) is now displayed in a blue -# font color (if the file is highlighted select a different file so yu can -# see the blue font color). That means that you have made changes to -# this file which have not yet been committed. -# -# COMMIT your work by selecting VCS from the menu bar, then select Commit Changes -# Make sure only the files you want to commit are checked and optionally -# add a quick Commit message to describe your work. Then hover over the -# Commit button and select Commit and Push. Commit saves the work to -# your computer. "and Push" saves a copy of your work up into your Github -# repository (saving to the cloud is a better way to permanently safe work). -# -# Oh, one more thing: -# Do you have any blue bars left on the on the scrollbar-like thing -# to the right? If so, click on each blue bar and change -# its TO DO to DONE. Then run the file (to make sure you didn't break -# anything) then Commit and Sync again. -# -# You can COMMIT as often as you like. DO FREQUENT COMMITS. -# -######################################################################## diff --git a/src/m2_todo_and_commit_push.py b/src/m2_todo_and_commit_push.py new file mode 100644 index 0000000..50a7e44 --- /dev/null +++ b/src/m2_todo_and_commit_push.py @@ -0,0 +1,123 @@ +############################################################################### +# +# This line is a COMMENT -- a note to human readers of this file. +# When the Python interpreter RUNS (i.e. EXECUTES) a program, it ignores +# everything from a # (hash) mark to the end of the line with the # mark. +# +# We call files that have Python code in them MODULES. +# Line 15 of this module (look at it now) prints (displays) the STRING +# Hello, World +# on the Run window. +# +# Anything surrounded by quote marks (single or double) is a STRING. +# +############################################################################### +print("Hello, World") + +############################################################################### +# TODO: 1. +# (Yes, that means for YOU to DO things per these instructions:) +# _ +# Run this module by right clicking anywhere in this window +# and selecting +# Run 'name of file' +# Or, use the keyboard shortcut: Control + Shift + Function-F10. +# _ +# After running, find the Run window and confirm that +# Hello, World +# did indeed get printed (displayed) on that window. +############################################################################### + +############################################################################### +# TODO: 2. +# Notice the small horizontal BLUE bars on the scrollbar-like thing +# on the right. Each blue bar indicates a thing "to do" in this module. +# _ +# a. You can use the blue bars to go from one _TODO_ to the next +# by clicking on the blue bars. ** Try that now. ** +# _ +# b. When you have completed a _TODO_, you should change the word +# _TODO_ (ignore the underscores on this line) +# to +# DONE +# Try it now on line 18 above, and note that its blue bar on the +# scrollbar-like thing to the right goes away soon thereafter. +# _ +# If you change each _TODO_ to DONE like this, you can tell when you +# have finished all the exercises in a module -- there will be no +# blue bars left on the scrollbar-like thing to the right. +# _ +# You have now completed _TODO_ #2, so change its _TODO_ on line 32 to DONE +# (and likewise for all forthcoming TODOs in this course). +############################################################################### + +############################################################################### +# TODO: 3. +# Add another print statement below this comment. +# It should print any string that you want (but keep it polite and G-rated!) +# _ +# Test your code by re-running this module, either by proceeding +# as you did when you ran this module the first time, +# or by pressing the green rightward-pointing triangle +# that is the "Play" button on the toolbar at the top of this window. +# Look at the Run window to be sure that your string printed as expected. +############################################################################### + +############################################################################### +# TODO: 4. +# Add yet another print statement, putting it below this comment. +# This one should print the *product* of 3,607 and 34,227. +# Let the computer do the arithmetic for you (no calculators!). +# You do NOT have to use strings for this, so no quotation marks! +# _ +# TEST your code by re-running this module, then asking someone +# whom you trust: +# What number did your print display for this _TODO_? +# (HINT: It is an INTERESTING number.) Get help if your value is wrong. +############################################################################### + +############################################################################### +# TODO: 5. +# Look at the list of files in the Project window (to the left). +# Note that this file: +# m2_todo_and_commit_push.py +# is now displayed in BLUE (or the color you may have set for changed files). +# _ +# The BLUE font color means that you have made changes to this file which +# have not yet been COMMITTED to version control and PUSHED to the cloud. +# _ +# COMMIT and PUSH your work by: +# 1. Select VCS from the menu bar (above). +# 2. Choose Commit from the pull-down menu that appears. +# 3a. In the Commit Changes window that pops up: +# - Just this once, type a short message in the +# Commit Message +# sub-box, e.g. "Done." +# It truly does not matter what message you put (but it WILL matter +# when you work with teammates later in the course). Subsequent +# commits will auto-apply whatever message you use this time. +# 3b: In that same Commit Changes window that pops up: +# - Press the Commit and Push button. +# (Note: If you see only a Commit button: +# - HOVER over the Commit button +# (in the lower-right corner of the window) +# - CLICK on Commit and Push.) +# _ +# COMMIT adds the changed work to the version control system +# on your COMPUTER. PUSH adds the changed work into your +# repository in the "cloud". +# _ +# Always PUSH (in addition to the COMMIT) so that your work +# is backed-up in the cloud. If you COMMIT but forget to PUSH, +# you can subsequently do the PUSH by: +# VCS ~ Git ~ Push... +# _ +# Oh, one more thing: +# Do you have any blue bars on the scrollbar-like thing to the +# right? If so, click on each blue bar and change its _TODO_ to +# DONE and then run the module (to make sure you did not break +# anything) and COMMIT-and-PUSH again. +# _ +# You can COMMIT-and-PUSH as often as you like. +# DO IT FREQUENTLY; AT LEAST once per module. +############################################################################### diff --git a/src/m3_names_and_expressions.py b/src/m3_names_and_expressions.py new file mode 100644 index 0000000..94903fd --- /dev/null +++ b/src/m3_names_and_expressions.py @@ -0,0 +1,117 @@ +answer = 2 ** 5 +print(answer * 100) + +############################################################################### +# TODO: 1. +# Read the 2 lines of code ABOVE this _TODO_. That code: +# 1. Computes 2 raised to the 5th power, +# yielding the object that is the integer 32. +# 2. Makes the name answer refer to that object. +# 3. Looks up the object to which the name answer refers (i.e., 32). +# 4. Multiplies that object (32) by 100 +# (hence computing the object that is the integer 3,200). +# 5. Prints the newly-computed object. (It prints WITHOUT the comma.) +# _ +# Make sure that you understand that those two lines do the above, +# ** ASKING QUESTIONS AS NEEDED. ** +# Once you completely understand the above, run this module, +# confirming that it prints 3200. Then change the above _TODO_ to DONE. +############################################################################### + +############################################################################### +# TODO: 2. +# Immediately below this _TODO_, write code that: +# - Computes 77 plus the cosine of 2.75. +# HINT: You will need to import the math module (library), +# by putting: +# import math +# at the top of this module, then using math.cos(2.75) in the code. +# - Stores that computed value using a name of your own choosing. +# - Prints the square root of that computed value. +# Run your code (fix errors as needed), then change the above _TODO_ to DONE. +############################################################################### + +############################################################################### +# TODO: 3. +# Immediately below this _TODO_, write code that computes and prints: +# the square root of ((41 * 88) + (4 * the cosine of 2)) +# Use as few or as many intermediate names as you feel appropriate. +# Run your code (fix errors as needed), then change the above _TODO_ to DONE. +############################################################################### + +############################################################################### +# TODO: 4. +# Immediately below this _TODO_, +# write code that computes the square root of 2 in two ways: +# 1. By using the math.sqrt function. +# 2. By raising 2 to the 0.5 power (using ** for exponentiation). +# Print both of the expressions that you write. +# Run your code (fix errors as needed), then change the above _TODO_ to DONE. +############################################################################### + +############################################################################### +# TODO: 5. +# Every object has a TYPE and a VALUE. For example, +# for the object that is computed by math.sqrt(2): +# Its TYPE is float (which is shorthand for "floating point number"). +# Its VALUE is (approximately) 1.4142135623730951. +# _ +# The TYPE of an object is important because it determines: +# -- what the object KNOWS, and +# -- what the object can DO. +# _ +# The type function returns the TYPE of its argument. For example, +# type(3.14) returns the CLASS (synonym for TYPE) 'float' +# and so the code: +# print(type(3.14)) +# will print +# Try it now! (That is, write +# print(type(3.14)) +# below this _TODO_ and run the program.) +############################################################################### + +############################################################################### +# TODO: 6. +# Now go through the BLAH objects listed below, one by one. For each: +# 1. First try to GUESS its TYPE. +# 2. Then write code of the form: +# print(type(BLAH)) +# 3. Then RUN the code to LEARN its TYPE. +# _ +# "hello" +# 'hello' +# "a b c" +# 3 + 3 +# "3" + "3" +# 2 ** 100 +# 2.0 ** 100 +# math.sin(8) +# math.sin +# print +# math +# "math" +# _ +# After you have written and run the code to learn the TYPE +# of each of the above, change the above _TODO_ to DONE. +############################################################################### + +############################################################################### +# TODO: 7. +# Ensure that no blue bars on the scrollbar-thing to the right remain. +# Run one more time to be sure that all is still OK. +# _ +# Then COMMIT-and-PUSH your work as before: +# 1. Select VCS from the menu bar (above). +# 2. Choose Commit from the pull-down menu that appears. +# 3. In the Commit Changes window that pops up, +# press the Commit and Push button. +# (Note: If you see only a Commit button: +# - HOVER over the Commit button +# (in the lower-right corner of the window) +# - CLICK on Commit and Push.) +# (Note: If it asks you to type a message for the Commit, do so, +# using Done or something like that for the message.) +# _ +# You can COMMIT-and-PUSH as often as you like. +# DO IT FREQUENTLY; AT LEAST once per module. +############################################################################### diff --git a/src/m3_turtles.py b/src/m3_turtles.py deleted file mode 100644 index df507f3..0000000 --- a/src/m3_turtles.py +++ /dev/null @@ -1,137 +0,0 @@ -""" -Demonstrates using OBJECTS via Turtle Graphics. - -Concepts include: - -- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS). - -- Make an object ** DO ** something by using a METHOD. - -- Reference an object's ** DATA ** by using an INSTANCE VARIABLE. - -Also: - -- ASSIGNING a VALUE to a NAME (VARIABLE). - -Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder, - their colleagues and PUT_YOUR_NAME_HERE. -""" -######################################################################## -# -# TODO: 1. -# (Yes, that means for YOU to DO things per these instructions:) -# -# On Line 13 above, replace PUT_YOUR_OWN_NAME_HERE with your OWN name. -# -# BTW, the top block of text above forms what is called a DOC-STRING. -# It documents what this module does, in a way that exterior programs -# can make sense of. It has no other effect on this program. -# -######################################################################## - -import rosegraphics as rg - -######################################################################## -# -# TODO: 2. -# Allow this file to use the rosegraphics.py file by marking the src -# directory as a "Sources Root". Do that by right clicking on the src folder, -# then selector Mark Directory As --> Sources Root -# Watch the red underlines on the line of code above disappear as you do that step. -# You will do that once for every project that uses rosegraphics so get used to it. :) -# -# Run this module by Right clicking in this window and select Run 'filename' -# A window will pop up and Turtles will move around. After the -# Turtles stop moving, *click anywhere in the window to close it*. -# -# Then look at the code below. Raise your hand as you have questions -# about what the code is doing. -# -######################################################################## - -# ---------------------------------------------------------------------- -# Set up a TurtleWindow object for animation. The definition of a -# TurtleWindow is in the rg (shorthand for rosegraphics) module. -# ---------------------------------------------------------------------- -window = rg.TurtleWindow() -window.delay(20) # Bigger numbers mean slower animation. - -# ---------------------------------------------------------------------- -# Makes (constructs) a SimpleTurtle object. -# ---------------------------------------------------------------------- -dave = rg.SimpleTurtle() - -# ---------------------------------------------------------------------- -# Ask the SimpleTurtle objects to do things: -# ---------------------------------------------------------------------- -dave.forward(100) -dave.left(90) -dave.forward(200) - -# ---------------------------------------------------------------------- -# Construct a new turtle and ask it to do things. -# ---------------------------------------------------------------------- -matt = rg.SimpleTurtle('turtle') -matt.pen = rg.Pen('red', 30) -matt.speed = 10 # Faster -matt.backward(50) -matt.left(90) -matt.forward(50) - - -######################################################################## -# -# TODO: 3. -# Add a few more line of your own code above to make one of the -# existing Turtles move some more and/or have different -# characteristics. -# -# ** Nothing fancy is required. ** -# ** A SUBSEQUENT exercise will let you show your creativity. ** -# -# As always, test by running the module. -# -######################################################################## - -######################################################################## -# -# TODO: 4. -# The code above CONSTRUCTS two SimpleTurtle objects and gives those objects NAMES: -# dave matt -# -# Below this TO DO comment construct another SimpleTurtle object, -# naming it whatever you want. -# Names cannot have spaces or special characters, but they can have -# digits and underscores like this_1_has (get it?). -# -# After you construct a SimpleTurtle, add a few more lines that -# make YOUR SimpleTurtle move around a bit. -# -# ** Nothing fancy is required. ** -# ** A SUBSEQUENT exercise will let you show your creativity. ** -# -# As always, test by running the module. -# -######################################################################## - -######################################################################## -# -# TODO: 5. -# Run one more time to be sure that all is still OK. -# Ensure that no blue bars on the scrollbar-thing to the right remain. -# -# Then COMMIT and Push your work using the VCS menu option. -# -# Reminder of those steps... -# COMMIT your work by selecting VCS from the menu bar, then select Commit Changes -# Make sure only the files you want to commit are checked and optionally -# add a quick Commit message to describe your work. Then hover over the -# Commit button and select Commit and Push. Commit saves the work to -# your computer. "and Push" saves a copy of your work up into your Github -# repository (saving to the cloud is a better way to permanently safe work). -# -# You can COMMIT as often as you like. DO FREQUENT COMMITS. -# -######################################################################## - -# ---------------------------------------------------------------------- -# This line keeps the window open until the user clicks in the window: -# Throughout this exercise, close_on_mouse_click should be the last line in the file. -# ---------------------------------------------------------------------- -window.close_on_mouse_click() diff --git a/src/m4_SimpleTurtle_objects.py b/src/m4_SimpleTurtle_objects.py new file mode 100644 index 0000000..1bdc24d --- /dev/null +++ b/src/m4_SimpleTurtle_objects.py @@ -0,0 +1,200 @@ +""" +Demonstrates using OBJECTS via Turtle Graphics. + +Concepts include: + -- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS). + -- Make an object ** DO ** something by using a METHOD. + -- Reference an object's ** DATA ** by using an INSTANCE VARIABLE. + +Also: + -- ASSIGNING a VALUE to a NAME (aka VARIABLE). + +Authors: David Mutchler, Vibha Alangar, Dave Fisher, Matt Boutell, Mark Hays, + Mohammed Noureddine, Sana Ebrahimi, Sriram Mohan, their colleagues and + PUT_YOUR_NAME_HERE. +""" +############################################################################### +# TODO: 1. +# Yes, that means for YOU to DO things per the following instructions: +# _ +# On Line 14 above, replace PUT_YOUR_NAME_HERE with your OWN name. +# _ +# BTW, the top block of text above forms a multiple-line comment that is +# called a DOC-STRING. It documents what this module does, in a way that +# exterior programs can make sense of. It has no other effect on this program. +############################################################################### + +import rosegraphics as rg + +############################################################################### +# TODO: 2. +# Allow this module to use the rosegraphics.py module by marking the +# src +# folder in this project as a "Sources Root", as follows: +# _ +# In the Project window (to the left), right click on the src folder, +# then select Mark Directory As ~ Sources Root. +# _ +# You will see that rosegraphics in the import statement above (line 27) +# is no longer marked as an error. You will do this in all projects +# that use rosegraphics, so get used to it. :) +# _ +# Once rosegraphics in the import statement is no longer marked as error, +# change this _TODO_ to DONE and ** continue to the next _TODO_ (below). ** +############################################################################### + +############################################################################### +# TODO: 3. +# Run this module. A window will pop up and Turtles will move around. +# After the Turtles stop moving, +# ** click anywhere in the window to close the window **. +# _ +# Then look at the code below. Ask for help when you have questions about +# what the code is doing, or if you are just curious about any of the code. +# Be sure that you understand the notations for: +# _ +# -- CONSTRUCTING an instance of a CLASS, e.g. +# rg.SimpleTurtle() +# _ +# -- ASSIGNING the resulting OBJECT (instance of a class) a NAME, e.g. +# natasha = rg.SimpleTurtle() +# _ +# -- Applying a METHOD to an object to make the object DO something, e.g. +# natasha.forward(100) +# _ +# -- Accessing an INSTANCE VARIABLE of an object, e.g. +# natasha.speed = 10 +# boris.speed = natasha.speed +# _ +# After you are confident that you understand all the code below, +# change this _TODO_ to DONE and ** continue to the next _TODO_ (below). ** +############################################################################### + +# ----------------------------------------------------------------------------- +# The next few lines show how to: +# - CONSTRUCT (make and initialize) a TurtleWindow object for animation. +# The definition of a TurtleWindow is in +# the rg (shorthand for rosegraphics) module. +# ----------------------------------------------------------------------------- +window = rg.TurtleWindow() +window.delay(20) # Bigger numbers mean slower animation. + +# ----------------------------------------------------------------------------- +# The next few lines show how to: +# - CONSTRUCT (make) a SimpleTurtle object and ASSIGN a NAME to the object. +# ----------------------------------------------------------------------------- +boris = rg.SimpleTurtle() + +# ----------------------------------------------------------------------------- +# The next few lines show how to: +# - Ask the SimpleTurtle object to do things by applying METHODs to it. +# The numbers in the parentheses are called ARGUMENTS. +# ----------------------------------------------------------------------------- +boris.forward(100) +boris.left(90) +boris.forward(200) + +# ----------------------------------------------------------------------------- +# The next few lines show how to: +# - Construct a second SimpleTurtle +# (using an optional argument that sets the shape displayed), +# set its pen and speed INSTANCE VARIABLES, and ask it to do things. +# _ +# TIP: to see what other strings you can use for the shape, hover over +# the word SimpleTurtle in the first line of code below. Doing so will +# pop up some quick documentation about whatever you hover upon -- try it! +# ----------------------------------------------------------------------------- +natasha = rg.SimpleTurtle("turtle") +natasha.pen = rg.Pen("red", 30) # Second argument is the thickness of the Pen +natasha.speed = 5 # Bigger means faster, max is usually about 10 + +natasha.backward(50) +natasha.right(90) +natasha.forward(125) + +natasha.speed = 1 # Now slower +natasha.go_to(rg.Point(-100, 200)) + +############################################################################### +# TODO: 4. +# Your instructor will explain the so-called "dot-trick". +# Try it out yourself by typing +# natasha. +# somewhere below and then pausing after typing the dot. +# You will see options pop up for what natasha can do (methods, marked +# with an M on the pop-up) and what natasha knows (instance variables, +# aka fields, marked with a F on the pop-up). +# _ +# Try out one of the methods! (Any is fine. Experiment!) +# _ +# ** Be quick to ask for help as needed, as the so-called ** +# ** "dot-trick" is more easily shown than explained. ** +# _ +# As always, test by running the module. +############################################################################### + +############################################################################### +# TODO: 5. +# Add a few more lines of your own code to make one of the existing +# SimpleTurtles move some more and/or have different characteristics. +# _ +# ** Nothing fancy is required. ** +# ** A SUBSEQUENT exercise will let you show your creativity. ** +# _ +# As always, test by running the module. +############################################################################### + +############################################################################### +# TODO: 6. +# The above code CONSTRUCTS two SimpleTurtle objects +# and gives those objects NAMES: +# boris natasha +# _ +# Add code of your own that constructs another SimpleTurtle object, +# naming it whatever you want. Names cannot have spaces or special +# characters, but they can have digits and underscores, e.g. +# this_1_has +# _ +# STYLE RULE: Your names should always begin with a LOWER_CASE letter. +# So mary is OK but Mary is NOT OK. +# Choose short-but-meaningful names. Separate "words" in the name by +# underscores, like this: my_turtle +# _ +# Then add more code that: +# -- Constructs a Pen object, +# -- Assigns your SimpleTurtle's pen to the constructed Pen object, and +# -- Makes your SimpleTurtle move around a bit. +# _ +# ** Nothing fancy is required. ** +# ** A SUBSEQUENT exercise will let you show your creativity. ** +# _ +# As always, test by running the module. +############################################################################### + +############################################################################### +# TODO: 7. +# Ensure that no blue bars on the scrollbar-thing to the right remain. +# Run one more time to be sure that all is still OK. +# _ +# Then COMMIT-and-PUSH your work as before: +# 1. Select VCS from the menu bar (above). +# 2. Choose Commit from the pull-down menu that appears. +# 3. In the Commit Changes window that pops up, +# press the Commit and Push button. +# (Note: If you see only a Commit button: +# - HOVER over the Commit button +# (in the lower-right corner of the window) +# - CLICK on Commit and Push.) +# (Note: If it asks you to type a message for the Commit, do so, +# using Done or something like that for the message.) +# _ +# You can COMMIT-and-PUSH as often as you like. +# DO IT FREQUENTLY; AT LEAST once per module. +############################################################################### + +# ----------------------------------------------------------------------------- +# The next line keeps the window open until the user clicks in the window. +# Throughout this exercise, this close_on_mouse_click line +# should be the LAST line in the file. DO NOT ADD CODE BELOW THIS LINE! +# ----------------------------------------------------------------------------- +window.close_on_mouse_click() diff --git a/src/m4e_loopy_turtles.py b/src/m4e_loopy_turtles.py deleted file mode 100644 index 168df44..0000000 --- a/src/m4e_loopy_turtles.py +++ /dev/null @@ -1,76 +0,0 @@ -""" -Demonstrates: - -- LOOPS (doing something repeatedly) and - -- using OBJECTS -via Turtle Graphics. - -Concepts include: - * Using OBJECTS: - -- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS). - -- Make an object ** DO ** something by using a METHOD. - -- Reference an object's ** DATA ** by using an INSTANCE VARIABLE. - - * LOOPS: - -- Using a FOR expresssion like this: - for k in range(41): - blah - blah - blah - - The above repeats the body of the FOR expression 41 times. - The name k is: - 0 the first time the body runs, - then 1 the next time the body runs, - then 2 the next time the body runs, - etc - then 40 the last time the body runs. - - * ASSIGNMENT and NAMES - -- ASSIGNING a VALUE to a NAME (VARIABLE), as in these examples: - jack = 45 - jill = 'ran down the hill' - size = size - 12 - -- The computer's STATE during a run of a program is the set - of current values of all the names that the computer is tracking. - - * The DOT trick: Type expressions like the following, - pausing after typing the DOT (period, full stop). - The window that pops up give lots of clues for what you can do! - rg. - rg.SimpleTurtle(). - rg.Pen(). - rg.PaintBucket() - -Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder, - and their colleagues. -""" -import rosegraphics as rg - -window = rg.TurtleWindow() - -blue_turtle = rg.SimpleTurtle('turtle') -blue_turtle.pen = rg.Pen('midnight blue', 3) -blue_turtle.speed = 10 # Fast - -# The first square will be 300 x 300 pixels: -size = 300 - -# Do the indented code 13 times. Each time draws a square. -for k in range(13): - - # Put the pen down, then draw a square of the given size: - blue_turtle.draw_square(size) - - # Move a little below and to the right of where the previous - # square started. Do this with the pen up (so nothing is drawn). - blue_turtle.pen_up() - blue_turtle.right(45) - blue_turtle.forward(10) - blue_turtle.left(45) - - # Put the pen down again (so drawing resumes). - # Make the size for the NEXT square be 12 pixels smaller. - blue_turtle.pen_down() - size = size - 12 - -window.close_on_mouse_click() diff --git a/src/m5_your_turtles.py b/src/m5_your_turtles.py deleted file mode 100644 index d9de2a6..0000000 --- a/src/m5_your_turtles.py +++ /dev/null @@ -1,30 +0,0 @@ -""" -Your chance to explore Loops and Turtles! - -Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder, - their colleagues and PUT_YOUR_NAME_HERE. -""" -######################################################################## -# TODO: 1. -# On Line 5 above, replace PUT_YOUR_NAME_HERE with your own name. -######################################################################## - -######################################################################## -# TODO: 2. -# -# You should have RUN the PREVIOUS module and READ its code. -# (Do so now if you have not already done so.) -# -# Below this comment, add ANY CODE THAT YOUR WANT, as long as: -# 1. You construct at least 2 rg.SimpleTurtle objects. -# 2. Each rg.SimpleTurtle object draws something -# (by moving, using its rg.Pen). ANYTHING is fine! -# 3. Each rg.SimpleTurtle moves inside a LOOP. -# -# Be creative! Strive for way-cool pictures! Abstract pictures rule! -# -# If you make syntax (notational) errors, no worries -- get help -# fixing them at either this session OR at the NEXT session. -# -# Don't forget to COMMIT your work by using VCS ~ Commit and Push. -######################################################################## diff --git a/src/m5e_loopy_turtles.py b/src/m5e_loopy_turtles.py new file mode 100644 index 0000000..1052777 --- /dev/null +++ b/src/m5e_loopy_turtles.py @@ -0,0 +1,97 @@ +""" +Demonstrates: + -- LOOPS (doing something repeatedly) and + -- using OBJECTS +via Turtle Graphics. + +Concepts include: + * Using OBJECTS: + -- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS). + -- Make an object ** DO ** something by using a METHOD. + -- Reference an object's ** DATA ** by using an INSTANCE VARIABLE. + + * LOOPS: + -- Using a FOR expression like this: + for k in range(41): + blah + blah + blah + + The above repeats the body of the FOR expression 41 times. + The name k is: + 0 the first time the body runs, + then 1 the next time the body runs, + then 2 the next time the body runs, + etc + then 40 the last time the body runs. + Note how the BODY of the loop (the code that is repeated 41 times) + is INDENTED. + + * ASSIGNMENT and NAMES + -- ASSIGNING a VALUE to a NAME (aka VARIABLE), as in these examples: + jack = 45 + jill = "ran down the hill" + size = size - 12 + + * The DOT trick: If you type expressions like the following, + pausing after typing the DOT (period, full stop), + then the window that pops up give lots of clues for what you can do! + rg. + rg.SimpleTurtle(). + rg.Pen(). + rg.PaintBucket(). + +Authors: David Mutchler, Vibha Alangar, Dave Fisher, Matt Boutell, Mark Hays, + Mohammed Noureddine, Sana Ebrahimi, Sriram Mohan, their colleagues and + PUT_YOUR_NAME_HERE. +""" +import rosegraphics as rg + +############################################################################### +# One window, for two examples. +############################################################################### +window = rg.TurtleWindow() + +############################################################################### +# Example 1. +############################################################################### +blue_turtle = rg.SimpleTurtle("turtle") +blue_turtle.pen = rg.Pen("midnight blue", 3) +blue_turtle.speed = 20 # Fast + +# The first square will be 300 x 300 pixels: +size = 300 + +# Do the indented code 6 times. Each time draws a square. +for k in range(6): + + # Draw a square of the given size: + blue_turtle.draw_square(size) + + # Move a little below and to the right of where the previous + # square started. Do this with the pen up (so nothing is drawn). + blue_turtle.pen_up() + blue_turtle.right(45) + blue_turtle.forward(10) + blue_turtle.left(45) + + # Put the pen down again (so drawing resumes). + # Make the size for the NEXT square be 12 pixels smaller. + blue_turtle.pen_down() + size = size - 12 + +############################################################################### +# Example 2. It shows how to speed up the animation. +############################################################################### +window.tracer(100) # Bigger numbers make the animation run faster + +another_turtle = rg.SimpleTurtle("triangle") +another_turtle.pen = rg.Pen("magenta", 1) +another_turtle.backward(50) + +# The name k takes on the values 0, 1, 2, ... 499 as the loop runs +for k in range(500): + another_turtle.left(91) + another_turtle.forward(k) + +window.close_on_mouse_click() diff --git a/src/m6_summary.py b/src/m6_summary.py new file mode 100644 index 0000000..d27c1b0 --- /dev/null +++ b/src/m6_summary.py @@ -0,0 +1,40 @@ +""" +An exercise that summarizes what you have learned in this Session. + +Authors: David Mutchler, Vibha Alangar, Dave Fisher, Matt Boutell, Mark Hays, + Mohammed Noureddine, Sana Ebrahimi, Sriram Mohan, their colleagues and + PUT_YOUR_NAME_HERE. +""" +############################################################################### +# TODO: 1. +# On Line 6 above, replace PUT_YOUR_NAME_HERE with your own name. +############################################################################### + +############################################################################### +# TODO: 2. +# Write code that accomplishes the following (and ONLY the following), +# in the order listed: +# _ +# - Constructs a SimpleTurtle with a "blue" Pen. +# - Makes the SimpleTurtle go straight UP 200 pixels. +# - Makes the SimpleTurtle lift its pen UP +# (so that the next movements do NOT leave a "trail") +# HINT: Use the "dot trick" to figure out how to do this. +# - Makes the SimpleTurtle go to the Point at (100, -40). +# - Makes the SimpleTurtle put its pen DOWN +# (so that the next movements will return to leaving a "trail"). +# - Makes the SimpleTurtle's pen have color "green" and thickness 10. +# - Makes the SimpleTurtle go 150 pixels straight DOWN. +# _ +# Don't forget to: +# - import rosegraphics and +# - construct a TurtleWindow +# [remember the required PARENTHESES for constructing an object!] +# at the BEGINNING of your code, and to +# - ask your TurtleWindow to close_on_mouse_click +# as the LAST line of your code. (Again, parentheses needed!) +# See the beginning and end of m5e_loopy_turtles for an example. +# _ +# As always, test by running the module. +# As always, COMMIT-and-PUSH when you are done with this module. +############################################################################### diff --git a/src/m7_challenge_your_turtles.py b/src/m7_challenge_your_turtles.py new file mode 100644 index 0000000..a524644 --- /dev/null +++ b/src/m7_challenge_your_turtles.py @@ -0,0 +1,31 @@ +""" +Your chance to explore Loops and Turtles! + +Authors: David Mutchler, Vibha Alangar, Dave Fisher, Matt Boutell, Mark Hays, + Mohammed Noureddine, Sana Ebrahimi, Sriram Mohan, their colleagues and + PUT_YOUR_NAME_HERE. +""" +############################################################################### +# TODO: 1. +# On Line 6 above, replace PUT_YOUR_NAME_HERE with your own name. +############################################################################### + +############################################################################### +# TODO: 2. +# You should have RUN the m5e_loopy_turtles module and READ its code. +# (Do so now if you have not already done so.) +# _ +# Below this comment, add ANY CODE THAT YOU WANT, as long as: +# 1. You construct at least 2 rg.SimpleTurtle objects. +# 2. Each rg.SimpleTurtle object draws something +# (by moving, using its rg.Pen). ANYTHING is fine! +# 3. Each rg.SimpleTurtle moves inside a LOOP. +# _ +# In this CHALLENGE problem, be creative! +# Strive for way-cool pictures! Abstract pictures rule! +# _ +# If you make syntax (notational) errors, no worries -- get help +# fixing them at either this session OR at the NEXT session. +# _ +# Don't forget to COMMIT-and-PUSH when you are done with this module. +############################################################################### diff --git a/src/rosegraphics.py b/src/rosegraphics.py index b40cb01..0566130 100644 --- a/src/rosegraphics.py +++ b/src/rosegraphics.py @@ -1,14 +1,14 @@ """ rosegraphics.py - a simple Graphics library for Python. + Its key feature is: -- USING this library provides a simple introduction to USING objects. Other key features include: -- It has a rich set of classes, methods and instance variables. - -- In addition to classes like Circles that are natural for - students, it has other kinds of classes like RoseWindow - and FortuneTeller to provide a richer set of examples - than "just" a graphics library. + In addition to classes like Circles that are natural for students, + it has other kinds of classes like RoseWindow and SimpleTurtle + to provide a richer set of examples than "just" a graphics library. -- It allows one to do a reasonable set of graphics operations with reasonable efficiency. The API mimics Java's Shape API for the most part. @@ -16,12 +16,12 @@ (the standard graphics libraries that come with Python). -- Unlike tkinter, it is NOT event-driven and hence can be used before students see that paradigm. (There is a behind-the-scenes - facilty for listening for and responding to events, + facility for listening for and responding to events, for those who want to do so.) -- It attempts to be as bullet-proof as possible, to make it easy for beginners to use it. In particular, it attempts to provide reasonable error messages when a student misuses the API. - -- It was inspired by zellegraphics but is a complete re-implemenation + -- It was inspired by zellegraphics but is a complete re-implementation that attempts to: -- Be more bullet-proof. -- Provide a richer set of examples for using objects. @@ -40,25 +40,6 @@ First completed version: September 2014. """ -# FIXME (errors): -# -- clone() does not really make a copy; it just makes a new one -# but without cloning all the attributes. -# -- _ShapeWithCenter claims that things like Ellipse are subclasses, -# but they are not at this point, I think. In general, need to -# deal with overlap between _ShapeWithCenter and _RectangularShape. -# KEEP both of them to have some classes have corner_1 and corner_2 -# while others have center and ... - -# FIXME (things that have yet to be implemented): -# -- Allow multiple canvasses. -# -- Better close_on ... ala zellegraphics. -# -- Keyboard. -# -- Better Mouse. -# -- Add type hints. -# -- Catch all Exceptions and react appropriately. -# -- Implement unimplemented classes. -# -- Add and allow FortuneTellers and other non-canvas classes. - import tkinter from tkinter import font as tkinter_font import time @@ -69,61 +50,14 @@ # All the windows that are constructed during a run share the single # _master_Tk (a tkinter.Tk object) # as their common root. The first construction of a RoseWindow -# sets this _master_Tk to a Tkinter.Tk object. +# sets this _master_Tk to a Tkinter.Tk object. # ---------------------------------------------------------------------- _master_Tk = None # ---------------------------------------------------------------------- -# At the risk of not being Pythonic, we provide a simple type-checking -# facility that attempts to provide meaningful error messages to -# students when they pass arguments that are not of the expected type. -# ---------------------------------------------------------------------- -class WrongTypeException(Exception): - """ Not yet implemented. """ - pass - - -def check_types(pairs): - """ Not yet implemented fully. """ - for pair in pairs: - value = pair[0] - expected_type = pair[1] - if not isinstance(value, expected_type): - raise WrongTypeException(pair) - -# ---------------------------------------------------------------------- -# Serialization facility -# ---------------------------------------------------------------------- - - -def _serialize_shapes(self): - """Returns a list of strings representing the shapes in sorted order.""" - # Idea: dump all the stats on all shapes, return a sorted list for easy comparison. - # Problem: the order in which keys appear in dictionaries is random! - # Solution: sort keys and manually print - shapes = [shape.__dict__ for shape in self.initial_canvas.shapes] - keys_by_shape = [sorted(shape) for shape in shapes] - - for k in range(len(shapes)): - shapes[k]['_method_for_drawing'] = None - shapes[k]['shape_id_by_canvas'] = None - - result = [] - for k in range(len(keys_by_shape)): - shape = shapes[k] - result.append([]) - for key in keys_by_shape[k]: - result[-1].append(str(key) + ":" + str(shape[key])) - result[-1] = str(result[-1]) - return "\n".join(sorted(result)) - -# ---------------------------------------------------------------------- -# RoseWindow is the top-level object. -# It starts with a single RoseCanvas. +# RoseWindow is the top-level object. It starts with a single RoseCanvas. # ---------------------------------------------------------------------- - - class RoseWindow(object): """ A RoseWindow is a window that pops up when constructed. @@ -136,7 +70,7 @@ class RoseWindow(object): or use any of its optional arguments, as in these examples: window = rg.RoseWindow(400, 300) # 400 wide by 300 tall - window = rg.RoseWindow(400, 300, 'Funny window') # with a title + window = rg.RoseWindow(400, 300, "Funny window") # with a title Instance variables include: @@ -146,8 +80,8 @@ class RoseWindow(object): widgets: the things attached to this window """ - def __init__(self, width=400, height=300, title='Rose Graphics', - color='black', canvas_color=None, + def __init__(self, width=400, height=300, title="Rose Graphics", + color="black", canvas_color=None, make_initial_canvas=True): """ @@ -224,8 +158,8 @@ def __init__(self, width=400, height=300, title='Rose Graphics', # -------------------------------------------------------------- self.mouse = Mouse() self.keyboard = Keyboard() - self.toplevel.bind('