0% found this document useful (0 votes)
68 views

Python Next Steps Homework 4 1

Uploaded by

daboizack7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Python Next Steps Homework 4 1

Uploaded by

daboizack7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Homework 4: Introducing functions

Python Next Steps

Homework 4: Introducing functions


1. Which two of the following statements are correct? [2]
⬨ Functions are blocks of code that can easily be reused
⬨ Functions make your code longer and more complex
⬨ Functions are essential and every computer program must include at least one
⬨ Functions can make it easier and quicker to update a program in the future

2. Examine the following code:


def addNumbers():
num1 = int(input(“Enter first number: ”))
num2 = int(input(“Enter first number: ”))
print(“Total =”, num1 + num2)

def squareNumber():
num = int(input(“Enter a number: ”))
print(“Number squared =”, num * num)

(D)
print(“============”)
print(“Main Menu”)
print(“1. Add two numbers”)
print(“2. Square a number”)
print(“3. Quit”)

def quitMessage():
print(“Goodbye!”)

# MAIN METHOD

choice = 0
while choice != 3:
displayMenu()
choice = int(input(“Enter your choice: ”))
if choice == 1:
(A)
elif choice == 2:
(B)
elif choice == 3:
(C)
else:
print(“Error, invalid choice”)

(a) Write the piece of code that should appear at the point marked (A) [1]
addNumbers()

1
Homework 4: Introducing functions
Python Next Steps

(b) Write the piece of code that should appear at the point marked (B) [1]
squareNumber()

(c) Write the piece of code that should appear at the point marked (C) [1]
quitMessage()

(d) Write the piece of code that should appear at the point marked (D) [1]
displayMenu()

3. Examine the following code, part of a turtle drawing program:


def square(size):
for counter in range(4):
turtle.fd(size)
turtle.rt(90)

def shape(sides):
for counter in range(sides):
turtle.fd(100)
turtle.rt(360/sides)

# MAIN METHOD

square(100)
square(75)
square(50)

(a) Write one more line of code that would produce the following output [1]

square(25)

2
Homework 4: Introducing functions
Python Next Steps

(b) Write line of code that would produce a hexagon (six sided shape) [1]
shape(6)

(c) Write the main method that would produce the following shape [4]

Shape(3)
Shape(4)
Shape(5)
Shape(6)

[Total 12 marks]

You might also like