Code Club Auckland

Making cool stuff with code!

Writing with Python

Go to http://repl.it/languages/Python

Working in repl.it

You should see a white area on the left, and a dark area on the right.

Write your code on the left, then click the 'play' button to run your code.

print("Hello")

Did that work? It should bring "Hello" (without speech marks) to the console.

If you make a mistake, you'll get an error message instead. Try leaving out one of the speech marks, like this:

print("Hello)

Error!

That won't work. An error message appears, and tries to tell you what the problem is. (But it's not always good at explaining.)

Cool, now you know Python!

ASCII Art

Let's try printing a picture made out of letters. (That's ASCII art).

print("A picture of a dog...")
print(" 0____  ")
print("  ||||  ")

Does it look like a dog? Maybe you can draw something better.

Math with text

You can kind of do math with text:

print("hello" * 5)

Try it.

If we add an empty space inside the speech marks, we'll get a space between the words. Can you see where I've added the space?


print("hello " * 5)

Some kinds of math won't work:

print("hello" - 3)

Error!

You can't do that. You'll have to delete that line before we carry on.

Challenge

What does the following program print to the screen? See if you can guess correctly before running the program.

print("ha "*4)
print("ba" + "na"*2)
print("He" + "l"*2 + "o" + "!"*10)

ASCII patterns

Now that you know how to do calculations on text, now what? Why is it useful? Well, let’s say you wanted to draw an ASCII art rectangle that is 30 characters long and 3 characters high. You could either draw it the hard way, like this:

#stop, don't actually type this out!
print("##############################")
print("##############################")
print("##############################")

Or you could save time and draw it the easy way, like this:

#this is the easy way!
print("#" * 30)
print("#" * 30)
print("#" * 30)

Both give you exactly the same rectangle printed to the screen.

You could even use calculations to make interesting patterns, like this wave:

print("/\  "*10)
print("  \/"*10)

That was much easier than typing out the whole pattern!

Challenge: Code a Scarf

Your best friend is having an 11th birthday party, and as a gift you've decided to code them a scarf! Use calculations wherever possible to make your own scarf pattern.

If you're feeling generous, you could even code them a cake (including candles) to go with it!

Scarf:

-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-
-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|

Cake:

 .  .  .  .  .  .  .  .  .  . 
 i  i  i  i  i  i  i  i  i  i 
##############################
==============================
##############################
==============================
##############################

Credits: This project was based on one from Code Club World.