Chapter 6: Orc functions

Start Here

Chapter 1: In which a cow surprises you 

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies

Chapter 9: The Doggy-Nator and List Comprehensions

Chapter 10: In which you Eval the Doggy


No, I do NOT agree to your terms!

The robbers are lying dead on the floor. The Moo-Inator walks towards a scared bank employee. “Where is the One Laptop? The one with the Power to Rule them All?”

“Don’t know man. IT section?”

“Where eez the It section?”

“Top floor. That’s where all the big bosses are.”

You walk up the stairs, only to see a thick manual land at your feet. It is followed by a stapler. You realize you are being attacked! A motley group of engineers, sales orcs and lawyers are attacking you.

They block your way. “You shall not pass! I say to you, YOU SHALL NOT PASS.”

“Jeez man, I just want to go to the toilet.”

“Oh. Why didn’t you say so? First door on the right.”

The orcs wait patiently for you to return, and then attack you. Arrghhh!


Now is a good time to introduce conditions in Python.

The if command allows you to run your code if only certain conditions are fulfilled. Eg,

Thing to note: The difference between ‘=’ and ‘==’.

‘=‘ says, this value equal to that. In the example above, number is equal to 1.

‘==’ does a comparison. It checks: Is this equal to that? In your Python window, type this:

‘==’ returns true of false, as we see above.

The if command works by checking this condition. In our example, it checks if number is equal to 1. Since it is the first time around, it prints “Number is 1”.

The if command can also have an else command, that is run if the condition isn’t true:

You can also have something called an ‘else if‘, shortened to elif in Python:

Now, it checks the first condition. If it is not true, it checks the second condition. If both aren’t true, it will fall to the last condition. Try the code with number = 3.

Because the villains in our story are inspired by Hollywood, they decide to attack one by one, to give you a chance to recover. The first to attack is the sales orc.

The power of pizza compels you!

Then comes the lawyer:

Because he isn’t an orc, your attack doesn’t work on him.

Now what do you do if you are surrounded by a bunch of enemies? Create a list with random enemies thrown in:

Make it random, just be sure to add a secretary in there somewhere. Now we need to loop over this list.

The for loop must be familiar by now. The next line may require an explanation. The for loop goes over each element in the list, and calls it ‘e’ (in my example).

It then checks: Is the current value equal to orc, and if it is, it will print our message. Of course, we need to do this for all the enemies. And don’t forget the secretary- we don’t want to kill her. I will show you how to deal with her as well. Here is the complete code:

Run the code, and you will get this:

This is a good time to introduce functions. If you are going to repeat a lot of code, it makes sense to put it in a function. This makes your code easy to read and follow. So if you get bored of fighting enemies, just call this function. Let’s call it Just_Deal_With_Them().

def is the command that tells Python you are declaring a function. Anything in the brackets () is what you are passing in; in this case, the enemy name. To show how this function is used:

As you can see, the code is much simpler now. Let’s see another example. If you remember, we used the lower() and title() functions? What if we get a list of names in ALL CAPS, and want to change them to normal?

The new thing is the return command. Functions can return a value. In this case, it takes a capital name, and returns the title version of it. Try it out:

How do we use this in practice? Say we have a list of names in all caps, and we want to copy the names to a new list:

We declare an empty list called names_small. We then loop over the names. This line requires an explanations:

We are calling a function within a function. append() is a function that adds values to a Python list. In that function, we are calling our own function Normal_Case. Normal_Case returns the normal version of the names, which are passed to names_small.append(), which adds the names to the list. The final result is, we get a new list with our names fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *