Python For GCSE
Section E Exercises

Task 1

Write a program that allows the user to enter their name. Design and print a suitable banner that adjusts to fit their name nicely. For example,

**************
* Their Name *
**************

Task 2

Write a program to analyse a sentence. You should assume that all words are separated by single spaces and there are no leading or trailing spaces. Your program should take a sentence from the user as input, it should then count the words and the vowels in the program.

Task 3

Write a program that allows the user to enter a message. Encrypt the message by swapping the first character with the second, the third with the fourth and so on.

  • To do this, you need to start by finding the length of the string that a user entered.
  • If the string length is an odd number, add a space or a randomly chosen character to the string to make an even number of characters.
  • Make a variable to store the result and assign an empty string to it. (result = "")
  • Write a for loop that counts from 0 to 1 less than the length of the string, going up in 2s.
  • Inside the loop, read an item from the string and the one after it. Add the two characters to your result, second one first.
  • After the loop, output the result.

Task 4

Design, code test and evaluate a system to accept and test a password for certain characteristics.

  • It should be at least 6, and no more than 12 characters long
  • The system must indicate that the password has failed and why
  • A message to indicate that the password is acceptable must be displayed.
  • Password strength can be assessed against simple criteria to assess its suitability; for example, a password system using only upper and lower case alphabetical characters and numeric characters could assess the password strength as:

WEAK if only one type used, eg all lower case or all numeric
MEDIUM if two types are used
STRONG if all three types are used.

For example

hilltop, 123471324, HAHGFD are all WEAK,

catman3 and 123456t are MEDIUM and

RTH34gd is STRONG

A message to indicate the password strength should be displayed after an acceptable password is chosen.

Task 5

Write a program that can work out the distribution of letters in a given string literal. The output from the program should be the letters of the alphabet and the number of instances of each letter in the given string.

  • Start by making a list of 26 integers.
  • Convert the user's input to upper case.
  • Use a loop to read each character in the string.
  • Find the ASCII number of the character using the ORD function.
  • Subtract 65 from the ASCII number. That is the list index you should add 1 to.
  • Use a loop to output your results like the example. Use the CHR function to output the alphabet letters – chr(loop counter +65) and the count for each letter.

Task 6

A pangram is a sentence that contains all of the letters from the alphabet.

You can add to your code from Task 5 to do this job for you.

Set a variable to True at the start of your program. Use a loop to read each number in the list you created in Task 5. If the number you read from the list is 0, make the variable equal to False.

Task 7

Write a program that allows the user to input a sentence that is stored as a string. Output the same sentence in lower case letters, upper case letters and then in title case (first letter of each word capitalised).

Task 8

Write a program that checks if the first and last words of a string are equal. The user should input the string that you want to check.

"Apples should be compared with apples" should give the result true.

Task 9

names = ["aguilark2876", "aguirreh5364", "alig8414",
         "allisonj9363", "alvaradoa2865", "alvaradoa9610",
         "alvaradoa6498", "alvaradoj7671", "andersonc8951",
         "andradeh8856", "archerb2300", "archerj5126",
         "arellanoj6883", "ariasg8320", "ariasl3249",
         "ariasm5578", "armstrongh8409", "arnolda6143",
         "arnoldb5269", "arroyoh4403", "avilab1118", "avilar7162"]

The list above contains user names for a school network. Each username is the student's surname, followed by their first initial, then their unique student number.

Write a program that reads the list and outputs the information in the following form,

K Aguilar, 2876
H Aguirre, 5634

Take care to correctly capitalise the correct letters.

Task 10

Write a program that allows the user to input a sentence that is stored as a string. Your program should replace every 3rd letter of the string with the word "moustache".

Task 11

Write a program that accepts a string input from the user. The purpose of the program will be to determine if the user's string contains the words 'cat' and 'dog' the same number of times.

'catdog' True
'catcat' False
'1cat1cadodog' True

Task 12

Write a program that accepts a string input from the user. The program will read the input and double up every character,

CAT becomes CCAATT

Task 13

An isogram is a word that contains no repeating letters. Use some of the techniques and code that you have done for previous tasks to find a quick solution to writing a program that tests a string to see if it is an isogram.

Task 14

Write a program that allows the user to enter a 13 digit bar code as a single string.

Your program should then determine if the check digit (last digit) is correct and the bar code is valid.

You work out the check digit from the first 12 digits of the number. You will need to convert the characters to strings to be able to do the arithmetic you see in the following algorithm,

totOdd = 1st + 3rd + 5th + 7th + 9th+ 11th
totEven = 3 x ( 2nd + 4th + 6th + 8th + 10th + 12th)
bigTotal = totOdd + totEven
IF bigTotal Mod 10 is equal to 0 THEN
   checkDigit = 0
ELSE
   checkDigit = 10 - (bigTotal Mod 10)
END IF

Task 15

Write a program that allows the user to input a string. Your program should output an acronym formed from that string.

Task 16

Set a string to the first line of the outrageous ditty below.

OH SIR JASPER DO NOT TOUCH ME!
OH SIR JASPER DO NOT TOUCH!
OH SIR JASPER DO NOT!
OH SIR JASPER DO!
OH SIR JASPER!
OH SIR!
OH!

Work out the most efficient program that could produce the remaining lines by manipulating the first string. The following line might help. It creates an array by splitting the string where the spaces are.

Task 17

Write a program that allows the user to input a string. Your program should say whether the string could be a binary number or not. A binary number consists of only 0 and 1 characters.

Task 18

Like the binary program, except for hex. A string of valid hex digits would make this easy to complete.

Task 19

The following pseudocode describes an algorithm for enciphering text using a Caesar Shift. It begins by converting the plain text message to upper case. This makes the process a little simpler. It then reads each character from the upper case plain text and examines the character's ASCII code. If the ASCII number denotes an upper case letter, it is shifted by the value of the variable shift and added to the cipher text variable. If the character is not a letter, it is added to the cipher text as it is.

plain ← "The secret is out."
shift ← 3
upper ← UPPERCASE(plain)
cipherText ← ""
FOR letter ← 0 TO upper.LENGTH - 1
   tmpASC ← ASCII CODE OF upper[letter]
   IF tmpASC > = 65 AND tmpASC <= 90 THEN
      tmpASC ← tmpASC + shift
      IF tmpASC<65 THEN tmpASC ← tmpASC + 26
      IF tmpASC>90 THEN tmpASC ← tmpASC - 26
      cipherText ← cipherText + CHARACTER(tmpASC)
   ELSE
      cipherText ← cipherText + upper[letter]
   END IF
END FOR
OUTPUT cipherText

Task 20+

The last task is taken from the ciphers section of this web site. Have a look at some of the other ciphers and write programs that encipher and decipher text using those ciphers.