Starting from:
$35

$29

Laboratory 8 Functions Solution

Learning objectives:

Writing functions.




Directed Activities:

Download Lab08.py .

After the completion of each activity, show your answers to an instructor and switch driver and navigator.




 
Fill in the missing code. String format() method

(Note: For this exercise have one partner “drive” for two of the problems.) In the function, formatPractice(), each print statement contains a variable enclosed in angle brackets. Replace the variables (including the brackets) with code so that the three statements print the output given below:

 
Python code: print(“It’s raining {1} and {0}.”.format (<a))

Expected output: It’s raining cats and dogs.

 
Python code: print(<b.format(2.3, .4567))

Expected output: 2.30 0.457

 
Python code: print(<c.format(3, 7.4589))

Expected output: Time left 03:07.46

 
Python code: print(<d.format(“Steph”, “Curry”, 43.75432))

Expected output: Steph Curry: 43.75




2. Making a secret code.

Do the part of Exercise 7 on p. 163 that encodes a simple cipher. For example, if the user enters the string "hello" and the key 3, the output should be "khoor". Note that letters late in the alphabet will "fall off the end" of the alphabet. Call the function encode().




3. Wrapping.

Exercise 8 on p. 163 discusses the problem of “drop[ping] off the end” of the alphabet. Modify your solution to the previous problem, calling your function encodeBetter(). This program will perform a circular shift of a character so that a string encodes to another set of characters. For example, if the user enters the string "xray" and the key 3, the output should be "audb".




4. List modification

It is possible to modify an object in a function without returning the object. Write a void function addTen(nums), that accepts as a parameter a list of numbers and modifies this list by adding ten to each number. A function, testTens(), already exists to test this function once it is written. The expected output is:




[5, 2, -3]

[15, 12, 7]







5. Chapter 6, Programming Exercise 11

Write the described void function, squareEach(nums). This function accepts as a parameter a list of numbers and modifies the list by squaring each number in the list. Plus write a function, sumOfSquares(), to test this new function before going on to the next activity.




6. Chapter 6, Programming Exercise 12

Write the described function, sumList(nums)  sum. This function accepts as a parameter a list of numbers and returns the sum of the numbers in the list. Add code to sumOfSquares() to test this new function before going on to the next activity.




7. Chapter 6, Programming Exercise 13

Write the described void function, toNumbers(strList). This function accepts a list of strings (that represent numbers, eg. list may contain [“3”, 5.7”, “2”]). The function modifies the list by converting each string to a number. Add code to sumOfSquares() to test this new function.




 

Upload the file to both student accounts.

Delete any files that you have put on the desktop. Empty the trash.

More products