Create a simple Quiz Module
- Open IDLE
- Open a New Window (File|New Window)
- Type in the following. Be very careful how you type. You must press TAB to indent the print “Correct” and print “Wrong” lines
When you’ve entered the above press F5 to run the program. You will probably get an error. A lot of programming deals with debugging, fixing the errors in the code
If you have an error, check the following:
- Have you used TABs to indent the two print statements
- DId you put == on the third line, or just =
- Did you remember the : at the end of the 3rd and 5th line?
Testing the Program
Once the program is running, you need to test it. Run it with the following test data
- Harry the Happy Hamster is hiding in the cheese
Improving the Program
Now improve the program. Add some more questions. Can you find out how to get it accept both paris and Paris as answers? (Use Google)
- Open IDLE
- Open a new window (File|New Window)
- Enter the following and save the file as nameage.py
Run the program (press F5) and check that it works correctly.
Programs v Conversations
Python conversations are useful if you want to find out something quickly, however programs are more useful. You should remember that a program is a list of instructions. This page will show you how to write a program and save it for later.
Click on File|New Window to bring up a Python Module window. Type in the following.
Save your Python Module on your M:Drive as jokes.py (You could make a Python Folder to keep everything in)
Now run the module by clicking on Run|Run Module, or pressing F5. You might be asked to save your work before continuing.
When you run the module, the shell will appear as follows. Press enter to see the answer to the questions.
Now, add some more jokes to the list. Email your modules to your friends so they can run them.
Looking at the conversation below, you might have expected that a + name1 would give you
Instead, the Python Shell has given an error. Python errors look something like the words in red below. Don’t worry about making errors when coding, expert programmers do this all the time.
The reason there is an error is because you are mixing two types of data: numbers and Strings.
Examples of Numbers
num = 32
x = 47.231
cost = 15.60
temp = -15
Examples of Strings
s = “I never saw a purple cow”
name = ‘Joe Bloggs’
Address = “32 Mayfield Way”
Note that Strings can be enclosed in double quotes ” or single quotes ‘
Python needs to be strict about the difference between Strings and numbers because it needs to know when 1 + 1 = 2 and 1 + 1 = 11
Enter the following statements into Python:
a = 3
b = 4
c = “This”
d = ‘5’
e = “12”
What output do you suppose the Python Shell will produce when you enter the following? Use the Shell to see if you’re right.
- a + b
- a + c
- c + d
- d + e
- a + b + c
- c + d + e
A Python Conversation
Here is an example of a Python conversation. Anything following the >>> prompt has been input by the user. Anything in blue has been output in reply by Python.
Discuss the following:
- What does 3*5 mean?
- What does 20/4 do?
- What output do you suppose b-a will produce?
- What output will name2+name1 produce?
- What output will a+name1 produce?
Open a Python shell and input the following:
- a = 4
- b = 5
- name1 = “Joe”
- name2 = “Sally”
- b – a
- name2 + name1
- a + name1
Discuss what happened when you input the above. Was it what you expected?
Open IDLE. You should see something like the following
You are about to have a Python Conversation. In conversation mode, you enter a statement, press enter, and get a reply straight away
- Click after the >>> and type print “Hello”. Press enter. What does IDLE do?
- Click after the >>> and type print 1+2. Press enter. What does IDLE do?
- Use IDLE to work out 4+7. What did you enter?
- What output does print 4*5 produce?
- What output does print “3 x 5 =”, 3*5 produce?
- What is the difference between entering print “3+5” and print 3+5?
- Use IDLE to work out 41 235 * 7 2446
- Enter the following on IDLE: print “Half of 50 is “,50/2. What is output?
I use IDLE as an IDE for teaching Python for two reasons: it’s simple, and its standard. The above exercise is intended to get students use to using IDLE. It’s also intended to allow them to make mistakes. Experienced programmers know the difference between “This” and This, beginners don’t. More important than syntax, though, they need to learn to make mistakes and correct them. Beginners are often frightened that they will “harm” the computer. They need to be encouraged to experiment and not to be afraid they are going to break things.