top of page

Topic 6: Problem solving with programming

PART 6- 6.6 SUBPROGRAMS

Bootcamp 1- Random!

Mike wants some code that generates a random integer and a random decimal number between 0 and 1. Can you finish this code off for him?

a) Copy the code into Python or the online version here: https://www.onlinegdb.com/online_python_compiler

import random
numrange=??????
numfloat=??????
print ("The random numi is:",numrange," and random float id",numfloat)

 

DONE? Prove you have done it by showing your teacher or loading it to Edmodo

Bootcamp 2- Maths Fun!

Your maths teacher has learnt that you can use Python to do some cool Maths calculations.

Can you create a program for him/her that:

   a) When user enters a decimal/float number it working out the Ceiling value, floor value and the square root of that number

   b) Generates the value of PI

a) Use Python or the online version here: https://www.onlinegdb.com/online_python_compiler

 

HELP: Use the following bits of code:    import math   math.ceil()    math.floor()     math.sqrt()     math.pi

DONE? Prove you have done it by showing your teacher or loading it to Edmodo

Bootcamp 3- Turtle Car!

We are now learning about graphics in Python. Open this powerpoint and complete the code. Undertsand it as you go.

a) Use Python or the online version here: https://www.onlinegdb.com/online_python_compiler

b) Follow the Powerpoint step by step and learn to use graphics in Python.

DONE?  You should have some Python code that draws a car. Take a picture of the CODE AND THE OUTPUT to prove you have done it by showing your teacher or loading it to Edmodo.

Bootcamp 4- My House

At an opening evening at school, a Year 5 pupil asks whether you can draw a house with this specification:

a) Triangle green roof

b) Orange house

c) A window filled in Yellow

d) A pink door

e) A 2nd window filled with Yellow

STEP 1) Open Python or the online version here: https://www.onlinegdb.com/online_python_compiler

STEP 2) The code belows draws a house. Copy it into Python and run it.

STEP 3) Can you now adjust the house to the specification above?

DONE? You should have some Python code that draws a house to specification. Take a picture of the CODE AND THE OUTPUT to prove you have done it by showing your teacher or upload/turn it init to Edmodo

import turtle
###################
turtle.penup()
turtle.goto(0,0)
turtle.fillcolor("Blue")
turtle.begin_fill()
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
###################
turtle.penup()
turtle.goto(20,50)
turtle.fillcolor("Yellow")
turtle.begin_fill()
turtle.pendown()
for i in range(4):
    turtle.forward(25)
    turtle.left(90)
turtle.end_fill()
###################
turtle.penup()
turtle.goto(90,0)
turtle.fillcolor("White")
turtle.begin_fill()
turtle.pendown()
turtle.forward(50)
turtle.left(90)
turtle.forward(75)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(75)
turtle.left(90)
turtle.end_fill()
###################
turtle.penup()
turtle.goto(0,100)
turtle.fillcolor("Black")
turtle.begin_fill()
turtle.pendown()
for i in range(3):
    turtle.forward(200)
    turtle.left(120)
turtle.end_fill()

 

Bootcamp 5- Subroutines - Procedures

In this bootcamp you will be given 3 examples of procedures and arguements and how they work. However Jenny needs some help wioth her division code. Can you help?

a) Copy the code into Python or the online version here: https://www.onlinegdb.com/online_python_compiler

 

#In this bootcamp we learn about subroutines such as procedures and functions
#STEP 1- A procedure is like a function but does not return a value e.g. no new varibale with calculations
def hello():
    print ("Hello world")
hello()

#STEP 2- A procedure can also have a parameter. Here is the parameter "Number" but it needs a value from somehwere)
def Squares(Number): #So the parameter here is "Number"
    for x in range(1,Number+1):
        print(x,x*x)
Squares(7) #So it makes the procedure "Squares" with the ARGUEMENT OF 7 
NumA =11 #This variable NumA is equal to 11
Squares(NumA)##So it makes the procedure "Squares" with the ARGUEMENT being the variable

#STEP 3- A procedure can also have multiple parameters. )
def Multiple(Num1,Num2):
    answer=Num1*Num2
    print("If you caculate",Num1," multiplied by ",Num2,"you get",answer)
Multiple(6,6)#Here there are two arguements in the procedure "Mulitple"

#QUESTION 1:
#Can you fix this code? Jenny wants to divide 12 by 3
def Divide(Num1,Num1):
    answer=Num1*Num2
    print("If you caculate",Num1," multiplied by ",Num2,"you get",answer)
Multiple(3,12)

DONE? Prove you have done it by showing your teacher or loading it to Edmodo

Bootcamp 6- Subroutines- Functions

Functions are totally like procedures but they return values into the main program

a) Copy all the code into Python or the online version here: https://www.onlinegdb.com/online_python_compiler

 

#STEP 1- So a function returns a result back to the main program unlike a procdure.
#So when we have an outcome of a subroutine (e.g. if Square=1 ) then we add the return
def Squared (Square): #so function defined and paramrter is Square which come from the input
    if Square==1:
        print ("Durr thats easy!")
        return ("1") # so here we are return the result of the run function for the main program to use
    else:
        Result=Square**2
        return (Result) # so here we are return the result of the run function for the main program to use
Value = int(input("What is the number? :"))
Answer=Squared(Value)#So the answer variable value is calculated by the function running with input
print ("Answer is: ",Answer)

 

#QUESTION 1
#Finish off this function that calculates the total age of a person based on how many months they have been alive


def monthsalive(?????):
    age=months*12
    ??????????????????
months=int(input("How many months have you been alive?: "))
??????????????
??????????????
print ("You have been alive for",answer,"years")

 

DONE? Prove you have done it by showing your teacher or loading it to Edmodo

Bootcamp 7- Local and Global Variables

A local variable only exists in a subroutine/function block
A global variable is a variable that can be used inside the subroutine/function block as well as outside

a) Copy the code into Python or the online version here: https://www.onlinegdb.com/online_python_compiler

b) Fix the problem in Question 1

x=5
print ("X is a normal variable and is equal to: ",x)
def calc():
    global x #Here we need to use x inside the function so we initiate the global call
    print ("Here is a local variable in the function called Y calcuting 6+6")
    y= 6+6
    answer=y
    print ("y, the local variable holds the value",answer)
    x=x+10
    print("Now I have added 10 to the global variable x so x is now",x)
    joined=x+y
    print ("If I join x and y together.... the answer is",joined)           
calc()

#QUESTION 1- Barry is having problems here.
#Can you sort the code out for him as he gets errors when he runs it. Somehting is missing
points=0
def score():
    points=points+50
    handicap=points+200
    print("Your handicap is",handicap)
score()

 

DONE? Prove you have done it by showing your teacher or loading it to Edmodo

In the following area you will:

6.6.1 be able to write programs that use pre-existing (built-in,library)

Random Module - import random

The supported functionality is as follows:

Function                               Description

random.randint(a,b)          Returns a random integer X so that a <= X <= b

random.random()              Returns a float number in the range of 0.0 and 1.0

Math module- import maths
The supported functionality is as follows:

Function                               Description

math.ceil(r)                           Returns the smallest integer not less than r
math.floor(r)                        Returns the largest integer not greater than r
math.sqrt(x)                         Returns the square root of x
math.pi                                 The constant Pi (∏)


Time module- import time

The supported functionality is as follows:

Function                               Description
time.sleep(sec)                   The current process is suspended for the given number of seconds, then resumes at the next line of the                                                          program

Turtle graphics module- import turtle

The supported functionality is as follows:

Function                               Description
turtle.forward(steps)          Moves forward (facing direction) for number of steps
turtle.back(steps)                Moves backward (opposite-facing direction) for number of steps
turtle.left(degrees)              Turns anticlockwise the number of degrees
turtle.right(degrees)            Turns clockwise the number of degrees
turtle.home()                        Moves to canvas origin (0, 0)
turtle.setpos(x, y)                 Positions the turtle at coordinates (x, y)
turtle.reset()                          Clears the drawing canvas, sends the turtle home and resets variables to default values
turtle.hideturtle()                 Makes the turtle invisible
turtle.showturtle()                Makes the turtle visible
turtle.penup()                        Lifts the pen up
turtle.pendown()                   Puts the pen down
turtle.pensize(width)            Makes the pen the size of width (positive number)
turtle.pencolor(color)           Sets the colour of the pen. The input argument can be a string or an RGB colour

 

6.6.1 other functions (no import needed)

Function                           Description
chr(integer)                     Returns the string which matches the Unicode value of integer.
input(prompt)                 Displays the content of prompt to the screen and waits for the user to type in characters followed by a new line.
len(object)                       Returns the length of the object, such as a string, one-dimensional or two-dimensional data structure.
ord(char)                          Returns the integer equivalent to the Unicode string of a single ‘char’.
print(item)                       Prints item to the display.
range(start, stop, step) The range() function is used to generate a list of numbers using step, beginning with the first and up to, but                                                   not including, the last.
round(x, n)                        Rounds x to the number of n digits after the decimal (uses the 0.5 rule).

6.6.1 be able to write programs that use user-devised subprograms (procedures,functions)

6.6.2 be able to write functions that may or may not take parameters but must return values, and procedures that may or may not take parameters but do not return values

def <procname>():                                                A procedure with no parameters
<command>

def <procname>(<paramA>, <paramB>):       A procedure with parameters
<command>

def <funcname>():                                                A function with no parameters
<command>
return (<value>)

def <funcname>(<paramA>, <paramB>):       A function with parameters
<command>
return (<value>)

6.6.3 understand the difference between and be able to write programs that make appropriate use of global and local variables 

bottom of page