Category Archives: Learn Python

6 Simple Quiz Program

Simple Quiz

Create a simple Quiz Module

  1. Open IDLE
  2. Open a New Window (File|New Window)
  3. Type in the following.  Be very careful how you type.  You must press TAB to indent the print “Correct” and print “Wrong” lines

ifParis

Debugging

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

Test Data

  1. Paris
  2. paris
  3. London
  4. 32
  5. 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)

4 Programs v Conversations

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.

jokes

 

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.

runmodule

 

 

When you run the module, the shell will appear as follows.  Press enter to see the answer to the questions.

runmodule2

 

 

Now, add some more jokes to the list.  Email your modules to your friends so they can run them.

3 Strings and Numbers

Looking at the conversation below, you might have expected that a + name1 would give you

4Joe

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.

Error

 

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

 

Exercise

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.

  1. a + b
  2. a + c
  3. c + d
  4. d + e
  5. a + b + c
  6. c + d + e

2 A Python Discussion

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.

shell1

Discuss the following:

  1. What does 3*5 mean?
  2. What does 20/4 do?
  3. What output do you suppose b-a will produce?
  4. What output will name2+name1 produce?
  5. What output will a+name1 produce?

Exercise

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?

1 Using IDLE

Open IDLE. You should see something like the following

Idle

You are about to have a Python Conversation. In conversation mode, you enter a statement, press enter, and get a reply straight away

  1. Click after the >>> and type print “Hello”. Press enter. What does IDLE do?
  2. Click after the >>> and type print 1+2. Press enter. What does IDLE do?
  3. Use IDLE to work out 4+7. What did you enter?
  4. What output does print 4*5 produce?
  5. What output does print “3 x 5 =”, 3*5 produce?
  6. What is the difference between entering print “3+5” and print 3+5?
  7. Use IDLE to work out 41 235 * 7 2446
  8. Enter the following on IDLE: print “Half of 50 is “,50/2. What is output?

 

Pedagogy

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.