Python practice 2
Python practice 2
Example Code
Please note: In the following examples, the terms word, phrase, name, firstname and
surname are all variable names.
len(word) word.upper()
Finds the length of the variable called word. Changes the string into upper case.
print(word.capitalize()) word.lower()
Displays the variable so only the first word
Changes the string into lower case.
has a capital letter at the beginning and
everything else is in lower case.
word.title()
name = firstname+surname Changes a phrase so that every word
Joins the first name and surname together has a capital letter at the beginning
without a space between them, known as with the rest of the letters in the
concatenation word in lower case (i.e.Title Case).
Challenges
020 021
Ask the user to enter Ask the user to enter their first name and then ask them to
their first name and enter their surname. Join them together with a space between
then display the and display the name and the length of whole name.
length of their name.
022
Ask the user to enter their first name and surname in lower
case. Change the case to title case and join them together.
Display the finished result.
023 024
Ask the user to type in the first Ask the user to type in any word and display it in
line of a nursery rhyme and upper case.
display the length of the string.
Ask for a starting number and an 025
ending number and then display Ask the user to enter their first name. If the length
just that section of the text of their first name is under five characters, ask
(remember Python starts them to enter their surname and join them
counting from 0 and not 1). together (without a space) and display the name
in upper case. If the length of the first name is five
or more characters, display their first name in
lower case.
026
Pig Latin takes the first consonant of a word,
moves it to the end of the word and adds on an
“ay”. If a word begins with a vowel you just add
“way” to the end. For example, pig becomes igpay,
banana becomes ananabay, and aadvark becomes
aadvarkway. Create a program that will ask the
user to enter a word and change it into Pig Latin.
Make sure the new word is displayed in lower case.
36 Challenges 35 - 44: For Loop
Example Code
The range function is often used in for loops and lists the starting number, the ending
number and can also include the steps (e.g. counting in 1s, 5s or any other value you wish).
for i in range(1,10):
print(i)
This loop uses a variable called “i” to keep track of the number of times
the loop has been repeated. It will start i at 1 (as that is the starting
value in the range function) and repeat the loop, adding 1 to i each time
and displaying the value of i until it reaches 10 (as dictated by the range
function), where it will stop. Therefore, it will not repeat the loop a tenth
time and will only have the following output:
1, 2, 3, 4, 5, 6, 7, 8, 9
Using loops is a
powerful programming
tool that you will use a
lot in the more
challenging programs.
for i in word:
print(i)
This would display each character in a
string called “word” as a separate
output (i.e. on a separate line).
Challenges 35 - 44: For Loop 37
Challenges
035 036
Ask the user to enter Alter program 035 so that it will ask the user to enter their
their name and then name and a number and then display their name that
display their name number of times.
three times.
037
038 Ask the user to enter their name and display each letter in
Change program their name on a separate line.
037 to also ask for a
number. Display 039
their name (one
Ask the user to enter a number between 1
letter at a time on
and 12 and then display the times table for
each line) and
that number.
repeat this for the
number of times
they entered. 040
Ask for a number below 50 and then count down from
50 to that number, making sure you show the number
041 they entered in the output.
Ask the user to enter their
name and a number. If the
number is less than 10, then
042
display their name that Set a variable called total to 0. Ask the user to enter
number of times; otherwise five numbers and after each input ask them if they
display the message “Too want that number included. If they do, then add the
high” three times. number to the total. If they do not want it included,
don’t add it to the total. After they have entered all five
numbers, display the total.
043
Ask which direction the user wants to count (up or down). If they select up, then ask
them for the top number and then count from 1 to that number. If they select down, ask
them to enter a number below 20 and then count down from 20 to that number. If they
entered something other than up or down, display the message “I don’t understand”.
044
Ask how many people the user wants to invite to a party. If they enter a number below
10, ask for the names and after each name display “[name] has been invited”. If they
enter a number which is 10 or higher, display the message “Too many people”.
Challenges 45 - 51: While Loop 41
Example Code
total = 0
while total < 100:
num = int(input(“Enter a number: ”))
total = total + num
print(“The total is”, total)
The above program will create a variable called total and store the
value as 0. It will ask the user to enter a number and will add it to
the total. It will keep repeating this as long as the total is still
below 100. When the total equals 100 or more, it will stop running
the loop and display the total.
Challenges
045 046
Set the total to 0 to start with. While the total is 50 or less, ask Ask the user to enter
the user to input a number. Add that number to the total and a number. Keep
print the message “The total is… [total]”. Stop the loop when asking until they enter
the total is over 50. a value over 5 and
then display the
047 message “The last
Ask the user to enter a number you entered
number and then enter was a [number]” and
another number. Add these stop the program.
two numbers together and
then ask if they want to add
another number. If they 048
enter “y", ask them to enter Ask for the name of somebody the user wants to invite
another number and keep to a party. After this, display the message “[name] has
adding numbers until they
now been invited” and add 1 to the count. Then ask if
do not answer “y”. Once the
loop has stopped, display they want to invite somebody else. Keep repeating this
the total. until they no longer want to invite anyone else to the
party and then display how many people they have
049 coming to the party.
051
Using the song “10 green bottles”, display the lines “There are [num] green bottles
hanging on the wall, [num] green bottles hanging on the wall, and if 1 green bottle
should accidentally fall”. Then ask the question “how many green bottles will be
hanging on the wall?” If the user answers correctly, display the message “There will be
[num] green bottles hanging on the wall”. If they answer incorrectly, display the
message “No, try again” until they get it right. When the number of green bottles gets
down to 0, display the message “There are no more green bottles hanging on the wall”.