Introduction to Programming
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP90059 Introduction to Programming
Lecture 5 Challenges
1. Write a function ascii_match(), which accepts two
required input arguments, an integer and a string. If
the ASCII numbers of all the characters in the string
add up to the value of the input integer, return True.
Otherwise, return False.
2. Write a function avg_three() that returns the average
(mean) of three integers between 1 and 9. The
function should allow for three optional input
arguments (num1, num2, num3), but if any of these
arguments is not provided when the function is
called, then a random number is generated for that
value.
COMP90059 Introduction to Programming 3
Notes
An update on the current plans for the Mid-
Semester Test (MST) and Assignment 1 (A1):
• A1 will be released via Grok on the Friday at
the end of Week 6 (April 8) and will be due 2
weeks after that.
• The MST will be held during the lecture period
of Week 7. It will be conducted online via LMS.
I will put up some sample questions soon.
• With MST, be careful in cut and pasting.
• Don’t worry.
• Discussion forum: tutors will also respond.
COMP90059 Introduction to Programming 4
Overview
• For Loops
• While loops
• Nested loops
• Good practice for writing code and the PEP8
standard
COMP90059 Introduction to Programming 5
Iteration
• Repeating or looping instructions to make
the computer do something repeatedly.
• Repeat something a fixed number of times:
• times a number by itself n times
• print each element in a collection
• print 7 instances of something
• Repeat something until something happens
(e.g., scroll while button held)
COMP90059 Introduction to Programming 6
Loops in Python
• The Python programming language provides
the following types of loops to handle
looping requirements:
• for loop - iterates over the members of a sequence
in order, executing the statement(s) inside each
time.
• while loop - repeats a statement or group of
statements while a given condition is true. It tests
the condition before executing the loop body.
• These loop types can also be nested: can
use one or more loops inside any another
COMP90059 Introduction to Programming 7
The for loop
• A for loop is used for iterating over Python
data collections (strings, lists, tuples,
dictionaries, sets)