So, we've been talking about collecting things together for a while. Lists, objects, etc. This should be the last term on that, and then we should start doing the really fun stuff! So, the last collectible programming term we're gonna be talking about is something called a module.
I'm sure we've gotten errors and all those, but have you ever read them? I've been able to understand what the red text says fairly well. And in EVERY error box, you will see this:
File "C:\Python33\Geometry.py", line 22, in <module>
See? It says module right there. Line 22, in <module>? Well, we can probably take a guess at what it is. All the programs we've done are simply modules. They can be used in a bigger program. So, a module is a piece of something! If you're still a little confused, take Lego blocks for example. Each little piece represents one of the simple programs we've written during our time at Programmer's Peak. You take the blocks, and use them to build a bigger construction. This construction is an entire program, and each piece is a simple module.
Modules are little programs that are used as pieces for big programs. Did we get that cleared up? So, why don't we try it? Let's create a new program, a simple little one. To make things easier, we might want to have the same file name. I saved mine as OurModule.py. This is where we get into Aarin's birthday theme. He said he wanted something with presents, so let's do that!
presentPossibilities=['Candy bar', 'Dragon', 'Can of beans', 'Skateboard', 'Hammer', 'Paintball gun']
chooser=0
number=5
Ha. Getting a dragon for your birthday would be the best thing ever (or not, depending on how dangerous it is), and a can of beans would be the worst. Well, guess what? We are going to make another program and shove this module in it!
So, how do we do that? Well, don't we understand some functions? They are normal English words. And the function we need is something we've used for quite a while now, to trigger random things and sleeping time. That function is import!
Let's open up a new window and do this:
import OurModule, time, random
print("Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.")
thing=input()
print("And the present is...")
time.sleep(2)
print("A", presentPossibilities[chooser], "!")
Well, what're you waiting for? Let's run it!
Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.
And the present is...
Traceback (most recent call last):
File "C:/Python33/AarinParty.py", line 7, in <module>
print("A", presentPossibilities[chooser], "!")
NameError: name 'presentPossibilities' is not defined
That was a disappointment.
Why'd this happen? Well, computers are dumb. You have to be this specific to make them work. Well, more specific. We never told our big program that presentPossibilities and chooser are part of Module.py! We'll have to fix it, like this:
print("A", OurModule.presentPossibilities[OurModule.chooser], "!")
Yay! Run the program and you should get this:
Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.
And the present is...
A Candy bar !
It works! Wait... Try running the program more than once...
Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.
And the present is...
A Candy bar !
Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.
And the present is...
A Candy bar !
Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.
And the present is...
A Candy bar !
Here's a problem! It doesn't stop doing a candy bar! That's my challenge. Try to debug Aarin's birthday party! Here's a tip: this is very similar to the program in the
A Quick Tip - The Randint of a List
article. It may point you in the right direction!
P.S. Don't be surprised if the modules article keeps going on. I may not have completely covered the topic yet.
No comments:
Post a Comment