0% found this document useful (0 votes)
6 views5 pages

CS1010E Week 04: Functions & Loops

The document outlines the content for Week 04 of a programming course, focusing on functions, selections, and repetitions in Python. It includes explanations of selection statements (if-elif-else), repetition statements (while-loop and for-loop), and various exercises related to function definitions, indentation, nested function calls, and turtle graphics. Additionally, it provides tasks for creating functions to calculate burger prices, sum odd numbers, and reuse code.

Uploaded by

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

CS1010E Week 04: Functions & Loops

The document outlines the content for Week 04 of a programming course, focusing on functions, selections, and repetitions in Python. It includes explanations of selection statements (if-elif-else), repetition statements (while-loop and for-loop), and various exercises related to function definitions, indentation, nested function calls, and turtle graphics. Additionally, it provides tasks for creating functions to calculate burger prices, sum odd numbers, and reuse code.

Uploaded by

fewew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS1010E: Programming Methodology

Week 04: Functions, Selections and Repetitions

Files Questions
• [Link] 1. Function Definitions
• [Link] 2. Indentations
3. Nested Function Calls
Coursemology 4. Burger
• Trainings > Tutorial > Week 04 5. Code Reuse
6. Sum of Odd Numbers
7. Turtle Graphics
8. Still Turtle Graphics

Python

Selection

The basic of selection statement is the if-elif-else statement. Since elif and else are optional,
we may have the following possible structures. <statement> is a Python statement which may be
another selection and/or repetition. <expression> is a Python expression which evaluates to a
value.

If

1 if <expression>:
2 <statement>

If-Else

1 if <expression>:
2 <statement>
3 else:
4 <statement>

If-Elif-Else

You can insert as many elif in between if and else. Additionally, the else is still optional.
1 if <expression>:
2 <statement>
3 elif <expression>:
4 <statement>
5 elif <expression>:
6 <statement>
7 else:

1
8 <statement>

Repetition

There are two main repetition statements in Python, namely while-loop and for-loop. <statement
> is a Python statement which may be another selection and/or repetition. <expression> is a
Python expression which evaluates to a value. <var> is a variable name.

While-Loop

1 while <expression>:
2 <statement>

For-Loop

1 for <var> in <expression>:


2 <statement>

Question 1: Function Definitions


Which of the following are valid function definitions? Select all that apply.

# A # B
def foo(a, b): def foo():
return a + a return

No Bracket, so wrong
# C # D
def foo: define foo(a):
return b return a

# E # F
def foo(a, b, c): def foo(a)
print(a+b+c) return a

2
Question 2: Indentation Makes a Difference

What is printed by the following program? Take note of the difference between functions foo1()
and foo2() .
def foo1(x):
if x < 10:
if x > 5:
print('oh')
else:
print('ya')

def foo2(x):
if x < 10:
if x > 5:
print('oh')
else:
print('ya')

foo1(5)
foo2(5)

Question 3: Nested Function Calls


What is printed by the following program?
def square(x):
return x * x

def double(x):
return x + x

print( double(square(2)) - square(double(2)) )

Question 4: Burger
Nowadays, we can customize our food when we order. For example, you can order your burger
with extra or no cheese. In this exercise, we will simulate a burger joint that allows customization
of burger.
Our representation of a custom burger is a string with the following code names for customiza-
tion:

Code Ingredient Price

'B' A piece of bun $0.5


'C' A slice of cheese $0.8
'P' A single patty $1.5
'V' A layer of vegetables $0.7
'O' A layer of onions $0.4
'M' A layer of mushroom $0.9

3
Figure 1: A simple burger 'BVPB' (left), a double cheese
burger 'BVCPCPB' (middle) and a big mac 'BPVBPVCB'
(right).

Write a function burger_price(burger) that takes in a string (str) representing a cus-


tomized burger and returns the price of the burger.

Sample Run #1

1 >>> burger_price('BVPB')
2 3.2

Sample Run #2

1 >>> burger_price('BVCPCPB')
2 3.2

Question 5: Code Reuse


Write the following three functions:
1. is_positive(x) returns True if the input parameter x is positive, or False otherwise.
2. is_odd(x) returns True if the input parameter x is odd, or False otherwise.
3. is_even_and_positive(x) returns True if the input parameter x is even AND positive,
or False otherwise. Try to reuse the functions you have written in points 1 and 2 above.
Note that in this question, 0 is considered to be neither a positive number nor negative number.

Question 6: Sum of Odd Numbers in a Given Range

Write a function sum_odd_numbers(low, high) to sum all the odd integers between low and
high (both inclusive). For instance, sum_odd_numbers(4, 13) returns 45 (= 5 + 7 + 9 + 11
+ 13).

4
Question 7: Turtle Graphics Again
Draw the following figure with Python Turtle. The triangle is an equilateral triangle with lengths
of 75 , 150 , 225 , and 300 .

You are not allowed to use pen up command pu() . You should use a loop to draw all the
triangles one by one (from smallest to biggest or vice verse).
NOTE: When submitting your program to Coursemology, remember to remove the line from turtle import * .
Otherwise, compilation on Coursemology may fail.

Question 8: Still Turtle Graphics


Draw the following figure with Python Turtle. The triangle is an equilateral triangle with lengths
of 250 .

As usual, you are not allowed to use pen up command pu() . You should use a loop to draw
all the triangles one by one.
NOTE: When submitting your program to Coursemology, remember to remove the line from turtle import * .
Otherwise, compilation on Coursemology may fail.

You might also like