Download ExerciseBook

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SECTION 1:
Introduction to Python
Programming Algorithms Computational Thinking
Print Statements
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that prints your full name to the screen
(2)
Write a program that prints your full name, your address and your
email to the screen on separate lines
(3)
Write a program to print a shopping list of five items to the screen
(one per line). For example, the output could look like
To buy:
***********
Bread
Milk
Butter
Sugar
Tea
(4)
Write a program that prints the following to the screen
*******
******
*****
****
***
**
*
2|Page
Programming Algorithms Computational Thinking
(5)
Write a program that prints the following to the screen
*******
******
*****
****
***
**
*
**
****
*****
******
*******
(6)
Write a program that prints the following to the screen
Menu del Dia:
____________________
Soupa
Paella
Pimientos
Patats alioi
Helado
____________________
Vino o cerveza o aqua
7€
3|Page
Programming Algorithms Computational Thinking
(7)
Write a program that prints the following to the screen. The
program should use one print statement.
Hint: use \n to get onto a new line
Menu del Dia:
____________________
Soupa
Paella
Pimientos
Patats alioi
Helado
____________________
Vino o cerveza o aqua
7€
(8)
Write a program that prints out the title and a verse of your
favourite song
4|Page
Programming Algorithms Computational Thinking
Maths Operators
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that declares two whole numbers. The program
should determine and print the sum and product of the numbers in
the following format:
The sum of the numbers is X
The product of the numbers is Y
(2)
Write a program that acts like a mini-calculator. The program
should create three variables, the first variable should store the
text “Welcome to the mini-calculator”, the second and third
variables should store numbers. The program should print the
welcome message and the results of adding, subtracting and
multiplying the numbers. For example, given the numbers 10 and 2
the following should print:
Welcome to the mini-calculator
10+2 = 20
10-2 = 12
10*2 = 20
(3)
Write a program that computes and displays the number of
seconds in a year. You may assume the year is not a leap year.
Remember: There are 365 days in the year, there are 60 seconds
in a minute and there are 60 minutes in an hour. There are 24
hours in a day.
(4)
Write a program that computes and displays the number of metres
in 1000 kilometres.
1 km = 100 meters
(5)
Write a program that converts a value in euro to its equivalent in
Canadian dollars. You can assume that 1€ = 1.25$.
5|Page
Programming Algorithms Computational Thinking
(6)
Write a program that takes any measurement in pounds and
converts it to kilograms (There are 2.2 pounds in a kilogram).
(7)
Write a program that that will evaluate the following powers:
1. 26
2. 654
3. 1043
To find the exponent we use **
So for 22 we would write :
2**2
(8)
In python we have two types of division, one that will evaluate to a
whole number and on that will evaluate to a decimal number.
Write a program that will evaluate the following:
1. 7/5
2. 7//5
Now that you know the difference between ‘/ ‘ and ‘// ‘ write a sum
that evaluates to this:
1. 342÷59= 5.796610169491525
6|Page
Programming Algorithms Computational Thinking
Maths
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that converts a value, given in degrees Celsius, to
its Fahrenheit equivalent (Fahrenheit = 9 / 5 * Celsius + 32).
(2)
Write a program that calculates the VAT on the value of a goods
item. The current VAT rate is 23%.
Assume that the product is 100%. So if VAT was to be added on
the total value of the product would be 123%.
How would you calculate this?
Price of product*1.(VAT Rate) or (Price of product/100)*(100+VAT
Rate)
(3)
Write a program that calculates the allowance for the distance
travelled for a salesperson. The current is 66c per km. For
example, if the distance is 100km the salesperson receives €66.
(4)
Write a program that declares two variables to store the values 12,
and 10. Perform the following calculations (using the variables you
have just created) and print the results of each calculation.
(a): 12%10
(b): 10%12
num_a = ....
(5)
Write a program that determines and prints each digit of a threedigit number. The program should output the following (assuming
the number is 250):
The first digit is 2
The second digit is 5
The third digit is 0
Hint: Think about what the modulus operator will do. If i have a
7|Page
Programming Algorithms Computational Thinking
number 56, if i say 56%10 i will get 6, if i say 56/10 i will get 5.
(6)
Write a program that determines and prints each digit of a fourdigit number. The program should use a single print statement but
should print the output in the following format given (for example)
the number 2787:
The digits in the four digit number 2787 are:
2
7
8
7
Think about what you done in the example above
(7)
In a cash register when you pay for something the computer tells
the casher how much to give back to the customer.
Write a program that will calculate how much change to give back.
Example:
Given €50, cost €22.48, change €27.52
(8)
In the question above the computer forgot to take a special
discount away, when you spend over €20 you get 25% off, change
the above program to get the right cost and change.
Example:
Given €50, cost €16.86, change € 33.14
8|Page
Programming Algorithms Computational Thinking
Maths based problems
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that declares three numbers and stores the value
47 in the first, 89 in the second and 10 in the third.
Print the values to the screen.
Create another variable called result that stores the value of the
first number multiplied by the second number.
result = (number1) x (number2)
Print the result to the screen.
Then store the value of the second number multiplied by the third
number in the variable called result.
result = (number2) x (number3)
Print the answer to the screen.
(2)
Write a program that calculate the value of the following
expression: 3 + 4 * 5
Hint: remember the BOMDAS rule,
Brackets of Multiplication Division Addition Subtraction.
Example: 7 * 5 + 9
Correct way: 7 * 5 = 35
35 + 9 = 44
Incorrect Way : 5 + 9 = 14
7 * 14 = 98
(3)
Write a program that creates four integer variables called, num_1,
num_2, num_3 and num_4. Each variable should store the value 10.
num1=10
9|Page
Programming Algorithms Computational Thinking
Perform the following arithmetic and check your answers are
correct.
Pen and Paper:
(1)
(2)
(3)
Multiply 10 x 10 x 10 x 10
Add 10 + 10 + 10, then multiply the answer by 5
Add 10 to the result of 10x2
Now write it in the program:
(1)
(2)
(3)
(4)
Multiply all numbers together (answer is 10000)
Add the first three numbers together and multiply by 5
(answer is 150)
Add the first number to the second number multiplied by
2 (answer is 40)
Write a program that calculate the value of the following
expression: 1 /2 + (3 * 4 /5)
Hint: BOMDAS
(5)
Write a program that calculate the value of the following
expression: (2 + 3/5) * (9/3 – 2)
Hint: BOMDAS
10 | P a g e
Programming Algorithms Computational Thinking
Inputs
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that performs addition.
The program should ask the user to enter two numbers and should
print the sum of the numbers to the screen with an appropriate
message.
Hint: make sure you change the String into an integer
int(variable)
(2)
Write a program that asks the user to enter (1) their name and (2)
their favourite colour.
The program should print out both Strings.
(3)
Write a program that asks the user to enter a two digit number.
The program should print out each of the digits in the number, in
reverse order. For example, if the user enters 23, the program
should print:
The number entered was 23.
The second digit is 3.
The first digit is 2.
Hint: you can use the modulus operator
(4)
Write a program that asks the user to enter two numbers.
The program should then print their sum and product to the
screen.
Reminder: Change the input to an integer
(5)
Write a program that asks the user to enter a value in degrees
Celcius.
It should then convert this value to its Fahrenheit equivalent
11 | P a g e
Programming Algorithms Computational Thinking
(Fahrenheit = 9 / 5 * Celcius + 32).
(6)
Write a program that asks the user to input a distance in kms.
The program should then calculate the allowance for the distance
travelled for a salesperson. The current is 66c per km. For
example, if the distance is 100km the salesperson receives €66.
(7)
In a cash register when you pay for something the computer tells
the casher how much to give back to the customer.
Write a program that will calculate how much change to give back.
The user can enter in the cost and the amount given.
Example output:
Total cost : €14.73
How much are you giving? € 20
Your change: € 5.27
(8)
In the question above the computer forgot to ask how much of a
discount to take off. Change the above program to get the right
cost, discounted cost and change.
Example:
Total cost : €14.73
How much is the discount? 5
Discounted Cost: €13.99
How much are you giving? € 20
Your change: € 6.01
12 | P a g e
Programming Algorithms Computational Thinking
String Functions
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that asks the user to enter two Strings. The
program should:
 Print each String
 Print the length of each String
 Print the first String in uppercase and the second in
lowercase
 Print the character at the second position in the first String.
Remember to use the methods that go with strings
(2)
Write a program that asks the user to enter a five-letter word.
The program should print out the word. The program should also
print each character in the word on a new line.
For example, given the String “Hello” the program should print:
Hello
H
e
l
l
o
(3)
Write a program that prints each of the characters of a word in
reverse (print the last char first). For example, given the String
“Hello” the program should print:
o
l
l
e
H
(4)
Write a program that asks the user to enter their first name and
their second name. The program should then print a welcome
13 | P a g e
Programming Algorithms Computational Thinking
message as follows:
Welcome! <first name> <second name>.
Reminder: print(output1,output2,output3)
(5)
Write a program that asks the user to enter a three digit number.
The program should print the sum of the individual digits in the
number.
For example, if the user enters 123, the program should print:
The sum of the digits is 6.
Hint: A string is made up of slots, so for example the String 123 is:
2
1
Slot 0
1
2
3
So if you want slot 1 for the String you would say:
variable_name[1]
The last character in the String can be referenced as either the
last position or -1.
(6)
Write a program that asks the user to input their first name, their
middle initial and their surname. The program should output their
name in the following format, irrespective of whether capital
letters were used or not:
Welcome! John A. Griffin
Remember to go back through all the String methods
(7)
In python we can combine the same data types together. For
instance we can say 1+1 or 1.5 + 3.4. We could do the same for
Strings. Lets say we have 2 Strings:
1.
2.
first=”John”
last=”Doe”
If we want to combine them we would write:
full=first+last
14 | P a g e
Programming Algorithms Computational Thinking
This would result in :
JohnDoe
Try this with your name.
(8)
Write a program that allows the user to input a string. Then using
the indexes of the string change all the vowels to ‘ ! ‘:
Example:
Enter a String : Mine
M!n!
In Python there are built in features like the .upper(), .lower()
methods. We also have a .replace() method.
To use it we will take the above example:
“Mine.replace(‘i’,’!’)”
M!ne
15 | P a g e
Programming Algorithms Computational Thinking
If Else Statements
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that checks to see if an age is greater than or
equal to 18. If it is, print out that the person can vote, otherwise
print that they cannot vote.
Remember the structure of if statement
if (condition):
#Do Something
else:
Remember the 4
spaces for the
indent.
#Do Something Else
(2)
Write a program that asks the user to enter a number. The
program should determine if the number is odd or even and print a
message to the screen. For example:
The number 111 is odd
The number 224 is even
Hint: think about what the modulus can do. How can we use it to
see if a number is even?
(3)
Write a program that asks the user to enter a three digit number.
The program should determine whether the entered number is less
than or greater than 500.
(4)
Write a program that asks the user to enter a value in degrees
Celsius and also a value in degrees Fahrenheit.
A conversion should take place so the values are in the same scale
and the largest one should be printed to the screen, in both in
Celsius value and Fahrenheit value.
Hint: Think about how you will choose which one is bigger. Say if
16 | P a g e
Programming Algorithms Computational Thinking
x > y is true, what would you print out first?
(5)
Write a program that asks the user to enter a three digit number
and print to the screen whether all digits are even, all digits are
odd or there is a mixture of add and even digits.
Hint: think about what the modulus can do. How can we use it to
see if a number is even?
(6)
Write a program that asks the user to enter a day of the week.
The program will check the day to see if you can have a lie in. You
can only have a lie in if the day is a weekend day.
A sample output would be:
What day is it?
Tuesday
You can’t have a lie in today
(7)
Monkey Trouble
There are 2 monkeys together. The zoo keepers have to be careful,
if the 2 monkeys are smiling or neither of the monkeys are smiling
then there is a problem. However if only one of the monkeys are
smiling then there is no problem.
Write a program to see if there is a problem
A sample output would be:
Is monkey 1 smiling?
No
Is monkey 2 smiling?
Yes
There is no problem
Hint: use Booleans. So No would be false and yes would be true
(8)
You are driving a little too fast, and a guard stops you. There are 3
things that the guard can do:
0: give you no ticket if the speed is less than 60km/h
1: give you a small ticket if the speed is between 61-80 km/h
17 | P a g e
Programming Algorithms Computational Thinking
2: give you a big ticket if the speed is between 81-100 km/h
Write a program that asks the user to input the speed of the car.
The program will then decide what type of tick needs to be issued.
Example:
What was your speed? 65
You have got a small ticket
18 | P a g e
Programming Algorithms Computational Thinking
If Else Statement
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a program that asks the user to enter a number between 1
and 12.
The program should then print out the corresponding month. For
example, the value 1 would result in January being printed and the
value 12 should result in December being printed.
(2)
Write a program that asks the user to choose from a menu with
values 1 for addition, 2 for subtraction, 3 for multiplication and 4
for division.
The program should then ask the user to enter two numbers and
perform the appropriate arithmetic.
Hint:
if ():
#Do Something
elif():
#Do Something Different
(3)
Write a program that checks to see what grade an exam mark
should be allotted. If the grade > 70 a mark of excellence should
be awarded, if the grade > 55 and < 70 a mark of merit should be
awarded and all other marks should be awarded a fail.
(4)
Write a program that asks the user to select an item from a menu.
The menu should display as follows:
1: Raining
2: Sunny
3: Snowing
19 | P a g e
Programming Algorithms Computational Thinking
4: Cloudy
The system should print an appropriate sentence for the
corresponding number, for example, if the user selects 1 the
program could print
Put your coat on, it is raining outside.
(5)
A website like Facebook has a login system. This login system has a
username and a password. Write a program that will allow the user
to enter a username and a password. Then if the username and the
password are correct then the user will be granted access to the
system.
The username is “JustAPerson”
The password is “Hello”
Sample output
Username: JustAPerson
Password: Hello
Welcome JustAPerson
(6)
Time can be read in two ways, a digital clock way that uses 12 hours
or a 24 hour clock.
Write a program that will convert a time from 12 hours to 24
hours:
Example:
What time is it? 1.30 pm
It is 13:30
What time is it? 9:45 am
It is 09:45
20 | P a g e
Programming Algorithms Computational Thinking
21 | P a g e
Programming Algorithms Computational Thinking
While loop
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write some Python code that will print out all numbers between 1
and 100 using a while loop.
Remember the structure of a while loop
while (condition):
#Do Something
(2)
Write some Python code that will print out the numbers 1 to 10
squared, i.e. 1, 4, 9, 16, …, 100 using a while loop.
Hint: The square of a number is just the number multiplied by
itself
Example 52 = 5 x 5 = 25
(3)
Write some Python code that will print out the times tables for the
number 5.
(4)
Write some Python code that will print out all even numbers
between 1 and 50.
Hint: think about what the modulus does, how can we use it to see
if something is even
(5)
Write some Python code that will print out all odd numbers
between 1 and 50.
Hint: Look at the last example
(6)
Write some Python code to check the user inputs a number greater
than 100. The code should then print the square of that number to
22 | P a g e
Programming Algorithms Computational Thinking
the screen.
Hint: use a loop to check if the number is less than 100
(7)
Write a program that prints the following to the screen five times
using a while loop
*******
******
*****
****
***
**
*
(8)
Write a program that asks the user to enter a five-letter word.
The program should print out the word. The program should also
print each character in the word on a new line. You must do this in a
loop.
For example, given the String “Hello” the program should print:
Hello
H
e
l
l
o
23 | P a g e
Programming Algorithms Computational Thinking
While loop
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write some Python code that will ask the user to enter a String.
The code should check that the string has a length of between 5
and 10 characters. Print the String and the length of the String
out.
Example:
Input String: Python
Output: Pyhton 6
(2)
Write a program to print a row of 5 stars to the screen.
(3)
Write a program that’s asks the user to enter a number n, the
program should then print a single star on n lines.
Remember to change the input to a number
(4)
Write a program that adds all sequential numbers, starting from 1,
until the sum is equal to or exceeds 100.
(5)
Write a program that asks the user to input a word. With this word
make a palindrome by writing the word backwards beside it.
Example:
What is your word? Hello
The palindrome is: HelloolleH
(6)
Write a program that takes a string input. Then take the first
three letters from the string and print them out 3 times. If there
is not enough letters then just take what is there.
Example:
Enter a word: Hello
24 | P a g e
Programming Algorithms Computational Thinking
HelHelHel
(7)
In a cash register when you pay for something the computer tells
the casher how much to give back to the customer.
Write a program that will calculate how much change to give back.
This program will keep on running until you tell it you are finished.
You will have to use a while loop to check if you enter yes or no to
see if you are finished.
Example output:
Total cost : €14.73
How much are you giving? € 20
Your change: € 5.27
Have you got another transaction? No
Goodbye
(8)
Given a string, if it starts with f the computer will return Fizz, if it
ends in a b the computer will return Buzz. Write a program that
accepts a string and will change every f to Fizz and every b to buzz
Example:
What is your input? ffbfb
Output: FizzFizzBuzzFizzBuzz
25 | P a g e
Programming Algorithms Computational Thinking
For loop
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write some Python code that will print out the numbers -10, -8, -6,
-4, -2, 0, 2, 4, 6, 8, 10 using a for loop.
Remember the for loop,
For variable in range(start, finish, step size):
(2)
The following Python code will print out the three times tables:
Can you print out the four times tables using a similar structure?
Modify your code to make your output more informative, i.e. your
output should be something similar to:
(3)
Try writing a program that will ask the user to input an integer,
between 0 and 12, and print the correct times table. Test with all
of the tables up to 12.
(4)
Write a program that prints the following to the screen five times
using a for loop
*******
26 | P a g e
Programming Algorithms Computational Thinking
******
*****
****
***
**
*
(5)
Write a program to print a row of 5 stars to the screen using a for
loop.
(6)
Write a program that adds all sequential numbers, starting from
50, until the sum is equal to or exceeds 1000, using a for loop.
(7)
Write a program that takes a string input and a number n. Then
take the first three letters from the string and print them out n
times. If there is not enough letters then just take what is there.
Example:
Enter a word: Hello
Enter a number: 5
HelHelHelHelHel
(8)
Given a string, if it starts with f the computer will return Fizz, if it
ends in a b the computer will return Buzz. Write a program that
accepts a string and will change every f to Fizz and every b to buzz
Example:
What is your input? ffbfb
Output: FizzFizzBuzzFizzBuzz
27 | P a g e
Programming Algorithms Computational Thinking
Lists
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Create a list called myList with the following six items: 45, 62.3,
“welcome”, True, 43, 756.
Remember how to create the list:
myList=[ first term , second term , . . . , last term ]
(2)
Create a list with elements of type String, int and float. Print out
the length of the list. Using a while loop can you go through the list
and print out the contents of it.
Remember a list can hold different types.
(3)
Create a list of integer numbers.
Using a loop sum the integers together and print the sum to the
screen. You should then modify all elements in your list to be
double their current value?
myList=[3,4,5]
first element is at slot 0 and the last element is at slot -1
so if I wanted 3 I would say myList[0], so how would you
(4)
Create a list of numbers which we will use to move a turtle around
the screen.
The list should have 20 values of numbers.
Create a new turtle and using a loop go through the list and move
the turtle forward by each element value in the list, then turn left
and move forward that amount again, before moving on to the next
element in the list.
(5)
Write Python statements to do the following:
1. Insert the value “cat” at position 3.
2. Insert the value 99 at the start of the list.
28 | P a g e
Programming Algorithms Computational Thinking
3. Find the element at position 5.
(6)
Write a program that has a list containing whole numbers. The
program should print the sum of the even numbers in the list.
Hint: Remember how we find even numbers using Modulus.
(7)
Write a program that allows the user to add elements into a list.
The program should print the average of all the numbers in the list.
Hint: To get a position in a list myList[i]
(8)
Write a program that the user has to input two different lists.
With these lists get the average of the two lists. Then add the two
lists together and get the average of the final list.
Hint:
To add two lists together we just write myList1+myList2
29 | P a g e
Programming Algorithms Computational Thinking
Dictionaries
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Create a dictionary with the number 1-10 representing their
corresponding letter (1:A, 2:B, 3:C,...). Print the contents of the
dictionary and its length.
Remember how to create a dictionary:
myDictionary = { }
myDictionary [ “ one “ ] = ‘aon’
(2)
Create a dictionary with the letters A-J as keys, with each key
corresponding to a person’s name beginning with that letter (you
should choose the names).
Print out the contents of the dictionary and then change the
values that the keys A and C are storing and reprint your
dictionary.
(3)
Using the dictionary created in Q2, use a loop to list out the values
stored in the dictionary.
Remember the structure of the loops:
for x in range ( start value , end value ):
#Do Something
While ( condition ):
#Do Something
(4)
Create a dictionary with the numbers 1-10 such that the value
stored is the index * 2.
For example, key 4 should have a value 8 and the key 9 a value 18.
Using a loop print out the sum of all the numbers stored.
Hint: you can use a loop to go through a dictionary
30 | P a g e
Programming Algorithms Computational Thinking
myDictionary= { }
for i in myDictionary:
#Do Something
(5)
Create a dictionary with the numbers 1-10 such that the value
stored is the index squared.
For example, key 4 should have a value 16 and the key 9 a value 81.
Using a loop print out the sum of all the numbers stored.
Remember: 52 = 5 x 5 = 25
(6)
Create a dictionary such that the key is a person’s PPS number and
the value is their date of birth representing with a string, for
example: 12122004. The program should ask the user to enter a
person’s name and then print out their date of birth in the format:
12/12/2004.
(7)
Create a dictionary that holds all the counties of Ireland. The
counties should be referenced by the province. Use it to print out
all the counties in a province.
To
hold
multiple
values
“key”:[“value1”,”value2”]
(8)
a
dictionary
we
write
Create a dictionary that holds all the counties of Ireland. The
counties should be referenced by the province. Use it to print out
all the counties in a province.
To
hold
multiple
values
“key”:[“value1”,”value2”]
(9)
in
in
a
dictionary
we
write
In a shop an inventory must be taken to keep track of what was
sold.
Create a dictionary with the following attributes:
1.
2.
3.
4.
Bananas : 400
Oranges : 34
Apples: 495
Peach : 392
31 | P a g e
Programming Algorithms Computational Thinking
5. Pears : 432
6. Pineapple: 5
Apply the following actions to the dictionary:
1. 48 bananas were sold
2. 10 pineapples were added.
3. The shop decided not to stock peaches anymore. Remove
peaches from the dictionary.
4.
32 | P a g e
Programming Algorithms Computational Thinking
Functions
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a Python function called mySchool() which will print details
about your school to the screen (you can choose what to print
inside the function).
Remember how to define a function:
def nameOfFunction():
#Do Something
(2)
Write a Python function called squareNo() which will print out the
square (number * number) of each number from 0 to 10.
(3)
Write a Python function called timesTables() that will print the
times tables of some number.
A function can take a parameter:
def nameOfFunction(x):
#Do Something with x
(4)
Write a Python function called stars() which will print a row of
stars to the screen. You can decide how many stars to print.
(5)
Write a Python function called menu() which will print a sample
restaurant menu of your choosing to the screen.
33 | P a g e
Programming Algorithms Computational Thinking
(6)
Write a Python function called numbers() which will ask the user to
input a number between 1 and 10 and print the word for that
number. For example, if the user enters 4, the program will print
four.
34 | P a g e
Programming Algorithms Computational Thinking
Functions
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a Python function that will take in two integer parameters
and prints the sum of these numbers, the difference of these
numbers and the product of these numbers.
A function can take more than one parameter:
def nameOfFunction(x,y):
#Do Something with x and y
(2)
Write a Python function that takes in three numbers and will check
to see which of the numbers is the biggest and which is the
smallest. The answers should be printed to the screen.
Hint: there are a 6 comparison statements in total,
First one:
if (x<y and y<z and y<z):
print(x,"is the smallest")
print(y)
print(z,"is the biggest")
(3)
Write a Python function that takes in a String as input. Using a
while loop go through each character in the String and print it to
the screen one at a time.
Remember how to loop through a String
while condition:
#Do something
(4)
Write a Python function that will take in a String and a number as
parameters and prints the inputted string the amount of times
corresponding to the number. For example, if the parameters are
Hello and 4 the program should print:
Hello Hello Hello Hello
35 | P a g e
Programming Algorithms Computational Thinking
Remember you will have to cast the number into an int
int(input(“Enter a number”))
(5)
Write a Python function called numbers() which accepts a number
between 1 and 10 as a parameter. If a number greater than 10 or
less than 1 the program should print an error message. Otherwise
it should print the word for that number. For example, if the user
enters 4, the program will print four.
Remember your code from Lesson 13 question 6
(6)
Write a Python function that accepts a single letter as a
parameter. If the letter is a vowel it should print a message to
state this. Otherwise it should print not a vowel.
Remember when comparing characters:
char==”a”
36 | P a g e
Programming Algorithms Computational Thinking
Functions
Add comments to all programs to explain what your code does.
If you get stuck make sure you ask. Work with the people beside you.
(1)
Write a Python function that will take in a number representing a
distance in centimetres. It should return the equivalent of the
number in metres (1m = 100cm).
Remember to return a value from a function follow this syntax
def somefunction()
return some value or variable
(2)
Write a Python function to take in a parameter of type String.
Your procedure should use a loop to go through each character of
the String and count them. It should return this total to a variable
outside the procedure and print it to the screen.
Hint: do this question using a for loop. Do not use the length
function.
(3)
Write a Python function that will calculate how much VAT to pay on
a purchase. Your procedure should take in a value and then add
23% VAT to it. The answer should be returned to the code outside
the procedure and the answer displayed on the screen with the
original value.
(4)
Write a Python function which will take in two numbers one
representing the speed of a car (in km/hr) and another
representing the time taken (in hrs) to travel a particular distance.
Your function should calculate and return the distance travelled
using the formula:
Distance = Speed * Time
(5)
Write a Python function that takes a list of whole numbers as a
parameter and returns their sum.
Remember you can pass a list the same way you can pass variables
37 | P a g e
Programming Algorithms Computational Thinking
(6)
Write a Python function that takes a dictionary as a parameter and
a key. The function should determine the value associated with the
key, or else a value of -1 if the value is not found.
(7)
A website is built using HTML strings like <i>HELLO</i> which
draws HELLO as HELLO. In this example the “i” tag makes <i> and
</i> which surrounds the word “HELLO”.
Write a program that will take in a name of a tag and a string to
enclose with the tags.
Example:
What is your tag? P
What is your string? Hello, how are you?
Your html code is: <p>Hello, how are you?</p>
(8)
Write a function that acts like cash register. The function should
take 2 parameters, the cost and the cash given. The function
should return the amount of change.
38 | P a g e