Download presentation
Presentation is loading. Please wait.
1
Lecture 23 – Practice Exercises 5
CMPT 120 Lecture 23 – Practice Exercises 5
2
Goals for Today! Practise hand tracing and commenting a program
Practise implementing an image processing program
3
Question 1 a) – Add comments:
import random def random_pie(pies): pie1 = random.choice(pies) pie2 = random.choice(pies) if pie1 == pie2: return (True, pie1, pie2) return (False, pie1, pie2) # Main part of program pies = ("blueberry", "apple", "pecan") pie_answer = random_pie(pies) if pie_answer[0]: print("You win a free " + pie_answer[1] + " pie!") else: print("You get a 5% discount on either " pie_answer[1] + " or " + pie_answer[2] + " pie!")
4
Question 1 b) Is pies A and pies B the same variable?
If not, what is pies A? What is pies B? What is the scope of pies A? What is the scope of pies B? import random def random_pie(pies): pie1 = random.choice(pies) pie2 = random.choice(pies) if pie1 == pie2: return (True, pie1, pie2) return (False, pie1, pie2) # Main part of program pies = ("blueberry", "apple", "pecan") pie_answer = random_pie(pies) if pie_answer[0]: print("You win a free " + pie_answer[1] + " pie!") else: print("You get a 5% discount on either " pie_answer[1] + " or " + pie_answer[2] + " pie!") A B
5
Question 1 c) Can you rewrite the function random_pie() such that it only has 1 return statement instead of 2? import random def random_pie(pies): pie1 = random.choice(pies) pie2 = random.choice(pies) if pie1 == pie2: return (True, pie1, pie2) return (False, pie1, pie2) # Main part of program pies = ("blueberry", "apple", "pecan") pie_answer = random_pie(pies) if pie_answer[0]: print("You win a free " + pie_answer[1] + " pie!") else: print("You get a 5% discount on either " pie_answer[1] + " or " + pie_answer[2] + " pie!")
6
raspberries.jpg Attribution: "Fir0002/Flagstaffotos"
Question 2 Given the image raspberries.jpg below, what does the code below output (approximately)? from PIL import Image rasp = Image.open("raspberries.jpg").load() # raspberries.jpg is 320 x 213 pixels print(rasp[160, 0][0]) print(rasp[0,212]) If you want to download this image, click on “Other resolutions: 320 × 213 pixels ” raspberries.jpg Attribution: "Fir0002/Flagstaffotos"
7
Question 3 What colour would a pixel with RGB value (0, 0, 50) appear to be? (A) Dark red (B) Light red (C) Dark green (D) Light green (E) Dark blue (F) Light blue
8
Question 4 Problem Statement:
Write a function that returns the colour of a given pixel as a string (using the table below) Sample input: (0, 255, 0) Function returns: “green” Possible return values: “red”, “green”, “blue”, “white”, “black”, “yellow” or “magenta”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.