< Prev  |  Home  |  Next >

Python ‘if-else’ Statement

What if you don’t want someone else using a program you’ve written? You could make the user login, so that you know they are allowed access. Let’s start with a similar program to the customised greeting program before.

This program is very similar to the custom greeting program we made in the last section, except this time we’ve used if and else to print one thing or another, depending on what the user entered as their name.

What is your name? Rowan
Hello Rowan!
Oh hi, I’ve missed you.
 

Or:

What is your name? Rowan's mum, just being nosy
Hello Rowan's mum, just being nosy!
The police have been alerted, and will be here in 3 minutes. I really wouldn’t want to be you right now.
 

We could make our program even more secure, by asking the user for the password. I’ll design the program first, because it get’s a little more complicated:

Flowchart for the password program

I told you! Actually, it’s not that complicated; you just have to follow the arrows, like before. It’s just that this time there are 3 possible outcomes and not 2:

Here’s the code:

As I’ve already said, there are 3 possible outcomes in this program. Notice how there is an ‘if’ statement for checking the password nested within another ‘if’ statement for checking the name. In this program, either the wrong name is entered:

What is your name? Dave
The police have been alerted, and will be here in 3 minutes. I really wouldn't want to be you right now.
 

Or the correct name is entered but the password is wrong:

What is your name? Rowan
But are you Rowan though?
What’s the password? letmein
Stop pretending to be Rowan. You’re better than that.

Or both the name and password are correct:

What is your name? Rowan
But are you Rowan though?
What’s the password? justinbeiber4eva2k13
Only joking. I knew it was you.

If we were to test this program, we could find out whether or not it works:

Data Expected Actual
name == "Dave" Police… message Police… message
name == "Rowan" pwd == "Dunno" Stop pretending… message Stop pretending… message
name == "Rowan" pwd == "justin…" Only joking… message Only joking… message
name == "rowan" pwd == "justin…" Only joking… message  "Police…" message

So now do you see the importance of testing? The last line in the test plan has uncovered a problem. Even though the program pretty much works properly, when you type in the correct name but without the capital ‘R’ the program thinks the wrong name has been entered. Remember ‘R’ is not equal to ‘r’ (or “R” != “r” in Python).

We can fix this in one of 2 ways. First we could change the ‘checking the name’ line to:

That’d fix it. Or we could be a bit cleverer, and make the name all lower case first, and then check it:

Try it. Now you can enter the name as “rowan”, “Rowan” or even “RoWaN”, and the name is still accepted. Our program is now much harder to break. It is robust.

Challenge

Here’s a program called B.O.B (Basic Output Being) that let’s you talk to him/her/it:

Run this program and you’ll soon realise that B.O.B isn’t quite as intelligent as he’d like to think he is. Your job is to modify this code to make B.O.B. V.2. Use if/else statements to react to the user’s responses. You can assume that the user will always answer ‘yes’ or ‘no’. For example, if the user says that they don’t want to know anything then they shouldn’t have to read what the computer has to say. Maybe the computer could be sad if the user didn’t like what the computer said.

Again, if this seems hard then look at the password program above. It’s amazing how similar they are.

< Prev  |  Home  |  Next >