210 likes | 359 Views
COMPSCI 101 S1 2014 Principles of Programming. 09 More loops and conditions. Recap. A list stores data as a sequence len () returns the number of elements in a list Each element of the list can be accessed We use a for loop to iterate through the contents of a list
E N D
COMPSCI 101 S1 2014Principles of Programming 09 More loops and conditions
Recap • A list stores data as a sequence • len() returns the number of elements in a list • Each element of the list can be accessed • We use a for loop to iterate through the contents of a list • Conditional statements (if, else, elif) • Check the conditions and change the behaviourof the program. • We must use indentation to define that code that is executed, based on whether a condition is met. COMPSCI101
Learning outcomes • At the end of this lecture, students should be able to: • developing and testing code that uses nested if…elif…else statements • generate random numbers by using the random.randint(a, b) function • print a list of strings • developing and testing code that uses a for loop • Examples: • Case study 1: the salary calculator • Case study 2: the dice generator • Case study 3: printing the contents of a list using a for loop • Case study 4: counting the total number of characters in a list COMPSCI101
9.1 Case Study: 1The Salary Calculator • Task: • Complete the calc_pay() function that takes as input two arguments: an hourly rateand the number of hours an employee worked in the last week. • Your function should compute and return the employee's pay. • Any overtime work (hours beyond 40) should be paid at 1.5 times the regular hourly rate. • Arguments: hourly rate and number of hours • Returns: weekly wage ERROR 350.0 475.0 >>> calc_pay(10.0, -5) >>> calc_pay(10.0, 35) >>> calc_pay(10.0, 45) COMPSCI101
9.1 Case Study 1 Developing code that uses conditions • Problem: 0 40 COMPSCI101
9.1 Case Study 1 Developing code that uses conditions • Using if-elif-else: True hours< 0? False ERROR hours <= 40? True False Calculate standard pay Calculate standard pay + overtime pay COMPSCI101
9.1 Case Study 1 Developing code that uses conditions • Calculating standard pay and overtime pay: • Standard pay: • Overtime pay COMPSCI101
9.1 Case Study 1 Developing code that uses conditions Try Demo (01) • The Salary Calculator: True hours< 0? False ERROR hours <= 40? True False Calculate standard pay hourly_rate * number_of_hours Calculate standard pay + overtime pay Standard pay + Extra hours * Overtime rate COMPSCI101
9.1 Case Study 1 Testing code that uses conditions Try Demo (01) • Test cases: 0 40 COMPSCI101
9.2 Random NumbersIntroduction Try • The random module • This module contains a number of random number generators. • The randintfunction returns a random integer in range [min, max] inclusive • The syntax for using the randint function is as follows: • Note: We must first import the module. randint(min, max) >>> import random >>> random.randint(1,5) 5 >>> random.randint(1,5) 2 NameError: name 'random' is not defined COMPSCI101
9.2 Random NumbersCase Study 2 Try Demo (02) • Task: • Write a program that simulates the throw of a fair dice by returning some random integer between1 and 6 inclusive. • Knowledge: dice: • Returns: a random integer between1 and 6 inclusive • Algorithm: COMPSCI101
Exercise 1 • How do you generate a random integer i such that ? • How do you generate a random integer i such that ? • How do you generate a random integer i such that ? COMPSCI101
9.3 Strings and Lists of StringsStrings and Characters • Strings and Characters • A string is a sequence of characters. String literals can be enclosed in matching single quotes (') or double quotes ("). • Python does not have a data type for characters. • A single-character string represents a character • len() • The len() function returns the number of characters in a string. letter = 'A' # Same as letter = "A" num_char= '4' # Same as num_char= "4" message = "Good morning" # Same as message = 'Good morning' 12 1 print (len(message)) print (len(letter)) COMPSCI101
9.3 Strings and Lists of Strings Lists of Strings • Lists of strings • Lists are constructed with square brackets, separating items with commas. • len() • With lists, len returns the number of elements in the sequence number_list = [10, 20, 30, 40, 50] fruit_list = ["apple", "banana", "orange"] string_list = ["two", "four", "six", "eight"] 4 3 4 print (len(['a', 'b', 'c', 'd'])) print (len(fruit_list)) print (len(string_list)) COMPSCI101
9.3 Strings and Lists of StringsPrinting the contents of a list • We use a for loop to iterate through the contents of a list. apple banana orange >>> print_list(["apple", "banana", "orange"]) >>> print_list(["abc", "abc"]) abc abc COMPSCI101
9.3 Strings and Lists of StringsPrinting the contents of a list Try Demo (03) • Algorithm: for item in string_list: Print the item print (item) COMPSCI101
9.4 Lists of Strings Case Study 4: Sum of string lengths • Task: • Complete the count_total_len()function that takes a list of strings as input and returns the total number of characters in a list of strings. • Arguments: a list of strings • Returns: total number of characters in a list • Cases: 6 count_total_len(["abc", "abc"]) 17 count_total_len(["apple", "banana", "orange"])) COMPSCI101
9.4 Lists of Strings Case Study 4: Sum of string lengths • Example: ["apple", "banana", "orange"] COMPSCI101
Try Demo (04) 9.4 Lists of Strings Case Study 4: Sum of string lengths • Algorithm: COMPSCI101
Exercise 2 • Exercise: • Write a max_of_three() function that takes three integers as input and returns the largest number among the three numbers. COMPSCI101
Summary • randint(min, max) • returns a random integer in range [min, max] inclusive • must import the random module • len(string) • returns the number of characters in a string. • len(List) • returns the number of elements in the sequence • Example of for loop syntax: for element in list_of_data: print(element) COMPSCI101