< Prev  |  Home  |  Next >

Python ‘for’ Loops

Let’s write a program to find out how awesome you are. I know you’re awesome, but It’d be nice to have some proof. Here’s a really simple program to find out your ‘awesome level’:

Remember how we have to cast the integer ‘awesome number’ into a string to print it, by using str(awesome)?

It’s pretty easy to cheat at this game though; the game just checks the length of your name, so you just have to have a really long name to be the best:

Please enter your name: Arby
Your awesome level is: 4

Please enter your name: Jensen Jay Alexander Bikey Carlisle Duff Elliot Fox Iwelumo Marney Mears Paterson Thompson Wallace Preston
Your awesome level is: 106

By the way, that name isn’t made up, it actually exists. Google it. Or Bing it if you like, I’m no snob. Still, he has a higher awesome level than you. But let’s make the game a bit more fun, by changing the algorithm (the series of steps used) to work out the awesome level. How about counting the number of vowels in the name. That’s fairer isn’t it?

This program loops (or iterates) through each letter of their name (for char in name — ‘char’ is just a variable name; we could call it anything) and adds 1 to their score if it’s a vowel. Notice the use of indentation to show what happens for each character, and to show what happens if a character happens to be a vowel.

Notice what happens if we type:

Please enter your name:
Your awesome level is: 0

The name contains 1 vowel, so the awesome level should be 1. Can you fix this problem? (Hint: you need to turn the character into lower case before you compare it.)

Please enter your name:
Your awesome level is: 1

We’re getting there, but how about working out an awesome percentage? That seems a little bit more professional, and you can compare your percentage with other people playing the game. So how might we work out an awesomeness percentage?

In our design, we can use a decision to show how this program works:

Flowchart for the awesome level program

I should explain the ‘weighting’ part of the design. If the user only has 2 letters in their name, and 1 isn’t a vowel, then it’s silly to only take 1% from their awesome level; instead, the algorithm should take 50% off for each letter that isn’t a vowel. That’s what the weighting part works out.

If you want the program to display the awesome level as an integer, you have to turn the level into an integer before you turn it into a string:

Let’s test it:

Data Awesome level expected Awesome level actual
name == "Arby" 25% 25%
name == "aRbY" 25% 25%
name == "Mr Awesome" 40% 40%
name == "aaeee" 100% 100%


Design, code and test a program called ‘Dr. Love’. The program should ask the user for 2 names, and calculate their compatibility. You can calculate a compatibility ’level’ or a percentage, and use whatever algorithm you want to do the calculation. For example, a lot of ‘love calculators’ check whether the two names have the letters ‘l’, ‘o’, ‘v’, ‘e’ or ‘s’ in them.

< Prev  |  Home  |  Next >