Don't like this style? Click here to change it! blue.css
So now we can make functions, loops, if/else blocks, play with strings in many ways, and teach ourselves pretty much any feature we need. This is already extremely powerful. Now we need to think a little bit more about converting these skills into meaningful programs.
This course exists, not to teach you Python syntax, but to teach you to quickly model the problems you face as you head out into careers as engineers. With that in mind let's look at the daily challenge.
The goal is to add all positive odds smaller than
n. So how do I think that out? I might start
with some scratch paper and write out some sample answers. Maybe I think of very similar goals that are close but
easier, like add all numbers smaller than
n. If that is still too hard I try an easier goal, display
all numbers smaller than
OK, so now that I can see all numbers less than "n" how do I add them? Try adding some on paper: 1 + 2 + 3 + 4 + 5
In my mind I add two at a time, and somehow I get some "partial sums" along the way.
If you really think this out you'll end up having to make another variable which keeps the
Now, I want to add only the numbers which are odd. That means I have to selectively add. Probably an
if statement and that modulo operation:
FINALLY, I want to make this into a callable function. That means convert my code into a block inside of a
def myfunc(n): call.
EXTRA-CREDIT If you want to push yourself a little bit farther, then head to Project Euler and try to use Python to solve the first problem (and eventually the first 10 problems).
You all have learned some amazing skills so far, but a lot of it is still shaky. Also, while having the awareness of some Python syntax is great, you need to be able to apply it to a real problem. Here is a cool trick you can use when thinking a problem out.
Pseudo-code is a way of sketching out what a program should do without having to worry about the precise syntax. You can outline a series of steps which can lead you from nothing to a working program.
Example: Print a receipt
Pseudo-Task 1: (from Book) "Magic Dates" are dates where the month * the day equals
the last two digits of the year. For instance:
03/05/15. Write pseudo-code
for a program
which consumes "mm/dd/yy" and decides if it is a magic date.
Pseudo-Task 2: Create some pseudo-code tasks to make a Tic-Tac-Toe game.
Design Task 3: Gather with 2-3 other students. Design a function which draws an ASCII christmas tree on the screen, let the function take in some parameters like height or base-height. Begin with Pseudo-Code. Really implement it in cloud9 (you can collaborate on a workspace or partner-code).
We're going to do something unexpected today: our first animations (in ASCII).
ASCII Task 4: Start a cloud9 workspace from cisc106/ascii-animation. From a fullscreen terminal/bash
python3 ascii.py and observe the true message.
I would like you to animate a "creature" taking a "random walk" on a 10 by 10 "board". With your mini-group, first as pseudo code, then in real life. This will probably take the rest of class. Split up the tasks roughly as follows (one person per task, test independently to save time):
Random Walk task A: (The randomizer) Create a function which spits out a random direction "up", "down", "left", "right".
Random Walk task B: (The display) Create a function which takes in
creatureY and displays a 10 by 10 board with a creature (an
* perhaps?) at the
Random Walk task C: (The logic) Create a function which takes in
direction ("up", "down", "left", or "right") then returns the new coordinates. This function should also check
to see if the creature is trying to leave the board.
Now you should try to put them altogether into one program, then use the ASCII animation code to animate 30 rounds of your random walking creature!
To help out here are some sample functions that might get you started:
Submit a working random walk animation! How many times did your creature end up in a corner? (We're modelling something here…)
Here are two "turtle apps" done through http://trinket.io:
Look up the "turtle" API and create something interesting!