@extends('template') @section('title') Lab 2: Parameter practice @stop @section('content') # Lab 2: Parameter practice Write the code for this part in a __new__ file, `lab02/parameters.py`. ## Note on Labelling The sub-tasks in this section are marked Partner A or Partner B to indicate how to work on them as a pair and when you should switch "drivers." One of the members of your team should be writing code for the parts marked Partner A and then the other member should write code for the parts marked Partner B, to ensure you both get hands-on practice with today's concepts. If you happen to be in a team of 3, just switch who is writing code for each different segment, but make sure all members of the team get a chance to write code. You may have to dictate some code to each other when one part depends on a previous part, but we've arranged the parts to minimize this. Of course, whoever is not writing code should be helping out by suggesting things to try and helping to proofread the code that's being written. ## Task 0. `rocks`: a zero-parameter function
Partner A
Write a *None* function called `rocks` that *prints* `cs111 rocks!` when invoked. This is called a zero-parameter function because it does not have any parameters. Even without any parameters, you still need the parentheses `()` when invoking the function. ```xml >>> rocks() cs111 rocks! ``` ## Task 1. `bunny`: a zero-parameter function
Partner B
Write a *None* function called `bunny` that *prints* a multi-line bunny when invoked. ```xml >>> bunny() ()() (-.-) o_(")(") ``` ## Task 2. Single Parameter Functions ### Task 2A.`customRocks`: a single parameter function
Partner A
Write a `None` function called `customRocks` that *prints* a customized message using the supplied argument. ```xml >>> customRocks('olivia') olivia rocks! >>> customRocks('wellesley') wellesley rocks! >>> customRocks('peppa the pig') peppa the pig rocks! ``` ### Task 2B. `diff21`: how far from 21
Partner B
Write a `None` function called `diff21` that *prints* the positive difference between the given number and 21. Hint: use python's built-in `abs()` function which returns the absolute value of a number. ```py >>> diff21(10) 11 >>> diff21(27) 6 >>> diff21(21) 0 ``` ### Task 2C. `banner`
Partner A
Write a `None` function called `banner` that takes a string parameter and *prints* two lines: the first line as 5 stars before the string and after the string, with one space on either side of the string, and the second line has 6 stars before the string and 4 stars afterward. (see examples below) ```py >>> banner('hello') ***** hello ***** ****** hello **** >>> banner('butter popcorn') ***** butter popcorn ***** ****** butter popcorn **** ``` You should test your `banner` function to make sure it works with **any** parameter (some examples shown below): ```py >>> banner('YO') ***** YO ***** ****** YO **** >>> banner('Come and play with us') ***** Come and play with us ***** ****** Come and play with us **** ``` ## Task 3. Functions with more than one parameter *Hint: For some of the tasks below, you can use python's built-in `abs()` function which returns the absolute value of a given number.* ### Task 3A. `customRocksRepeat`: a multiple parameter function
Partner B
Write a `None` function called `customRocksRepeat` that accepts two parameters: `message` and `n` This function should *print* the message repeated by `n` times. Note: scroll right in the box below to see all the output ```py >>> customRocksRepeat('olivia', 1) olivia rocks! >>> customRocksRepeat('wellesley', 2) wellesley rocks! wellesley rocks! >>> customRocksRepeat('peppa the pig', 5) peppa the pig rocks! peppa the pig rocks! peppa the pig rocks! peppa the pig rocks! peppa the pig rocks! >>> customRocksRepeat('yo', 10) yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! ``` Peppa the Pig: A cheery cartoon pig wearing an orange shirt and black shoes. ### Task 3B. `diff`:
Partner A
Write a `None` function called `diff` that accepts two numbers and prints the positive difference between them. ```py >>> diff(5,21) 16 >>> diff(20,17) 3 >>> diff(100,-19) 119 >>> diff(4,4) 0 ``` ### Task 3C. `fruityDiff`:
Partner B
Write a `FRUITFUL` function called `fruityDiff` that accepts two numbers and *returns* the positive difference between them.
Note: Thonny's shell displays the results of fruitful functions in blue, left-aligned text. (but not with a None function). Contrast the sample output below with the sample output above in Task 3B. ```py >>> fruityDiff(5,21) 16 >>> fruityDiff(20,17) 3 >>> fruityDiff(100,-19) 119 >>> fruityDiff(4,4) 0 ``` ### Task 3D. `multDiff`:
Partner B
Write a `FRUITFUL` function called `multDiff` that accepts three numbers and *returns* the product of the positive difference between the first and second numbers and the positive difference between the second and third numbers. Hint: `multDiff` can invoke `fruityDiff`. Note: Thonny's shell displays the result of a fruitful function left-aligned and in blue. It's a subtle difference, but note how Thonny displays the side effect of a None function (`diff`) versus the returned value of a fruitful function (`fruityDiff`): The difference between a side effect and a returned value in the Thonny shell is subtle. A side effect is shown in slightly smaller black text, while a returned value is shown in a larger font and is colored according to its type (dark blue for numbers in this case). In some other consoles, there is no visual difference between what is printed after a statement like `>>> print(55)` and an expression like `>>> 55`, so one would have to remember whether an output is a side effect or a result based on what statement/expression they wrote.
Detailed rules about what is displayed Although this can be confusing, the rules for what is displayed in the shell when you evaluate an expression are straightforward: Because the print function returns the special value None, if you just call print, only the printed output will be displayed, because of the rules above. Similarly, if you call a non-fruitful function, only what it prints will be displayed, while if you call a fruitful function, first whatever it prints will be displayed, followed by its return value (because the value of a function call as an expression is the return value of that function). The two rules above only apply to code types in the shell. For code in a file, the second rule is skipped. Finally, the way in which returned values are displayed is different from printed values: their representations are displayed instead of their string equivalent. This is most obvious when it comes to values which are strings already: when printed, their characters will appear as-is, but when displayed as a result value, quotation marks will appear to indicate that the value is a string. Notice the difference:
>>> print("hello")
hello
>>> "hell" + "o"
'hello'

Some examples for `multiDiff`: ```py >>> multDiff(1,5,10) # note: 4 * 5 = 20 20 >>> multDiff(8,2,4) ?? What do you expect? >>> multDiff(10,-2,5) ?? ``` ## What's Next? At this point, you should move on to **either** part 3A or part 3B. You and your partner will have to decide whether you're more interested in writing functions that use `turtle` graphics or `wavesynth` audio. @include('/labs/lab02/_toc') @stop