What if?…or else?

We now enter the second week of this course. If you’ve not completed the first week, don’t panic! You’re probably not alone and no points are deducted for working at your own pace. Plus, now that there are others working ahead of you, they can help you out. Remember if you are having problem understanding a concept, don’t fret. You can always ask fellow Pythonistas over at the discussion forums. I am always there too.

Ok, let’s just dive right in. This week we will be asking ourselves some serious questions.

We are going to learn about conditionals, as the programmers call them. What are they? I imagine them like this:

IF time_right_now > 9_am:
  get_up_and_make_breakfast()
ELSE:
  push_snooze_button()

Another example of another type of conditional is:

WHILE chocolate:
  take_a_piece_and_eat_it()

And this type, which is very handy and used a lot:

FOR every_kitten IN the_litter:
  every_kitten.pet()

In programming these types of questions enable us to select the right variable. For example, our Tetris game is always throwing shapes from the top but how does the computer know when to clear the line or multiple lines once we have filled it nicely throughout? Well, the code could look something like this:

if line_filled:
  clear_line()

This week we will get to know some other cool stuff, besides conditionals, which will come in handy. So here’s what’s up for this week:


Reading

Get to know some “smooth” operators and conditionals in sections 4.1, 4.2, 4.4, 4.5, 4.6, 4.7, 6.1 & 6.2 of the online textbook How to Think Like a Computer Scientist.

Trying it out

I like these Codecademy exercises very much, how about you? For this week I thought it would be great to get to know a bit more about Conditionals and Control Flow – (Unit 3)

Exercises

From the MIT OCW’s course A Gentle Introduction to Programming Using Python

  • Complete sections 1.5, 1.6 and 1.8 and 1.7 and optional exercises from Handout 1

  • Complete sections 1.13, 1.14, and 1.15 from Written Exercises 1

Project

In our tetris.py file, let’s start by asking ourselves what shape we want the program to print out on our IDLE console. To do this, we’ll need to change the code, so that when we run our program, the computer will be able to ask about what shape we would like it to print next. If the answer is for example “J”, it should print something like this: _|. If you have a nicer looking shape or some with greater complexity, then come to our [discussion forums] (http://discourse.p2pu.org/c/gentle-introduction-to-python){:target=”_blank”} quickly and tell us how you upgraded the game and solved this problem.

Game on!

moocie face
–Your mechanical friend MOOC-E