Chapter 16 Exercises¶
Fix syntax 6 errors in the code below so that the code runs correctly. It should set
combinedto the concatenation ofstartandname. It should print the length of the combined string, print the combined string, and it should print the result ofname * 3.Change the first
'to a"in line 1. Change the"to a'in line 2. Add a+afterstartin line 3. Add a)at the end of line 4. Add a(and)on line 6.Add code to line 1 so that the code below prints 14.
aStringcan be any 4 character stringFix the 5 syntax errors in the code below so that it runs. It should print the length of
myFirstListand print the result ofmyFirstList * 3. Then it should setmySecondListto the concatenation ofmyFirstListand a list containing321.4. Then it should print the value ofmySecondList.Add a comma before the
13on line 1. Add a)at the end of line 2. Change line 3 tomyFirstList. Add a]at the end of line 4. Add a)at the end of line 5.Fix the errors so that the procedure prints “My name is JohnJohn” and “19”.
Add quotations around John on line 8. Add a space after “is” in line 2. In line 3, change the
*after start to a+. In line 5 add a)after combined. Indent lines 2 through 5.Fix 5 syntax errors in the code below so that it runs and prints the contents of
items.Add a
,between 6 and 8 on line 7. Change the'to a"on line 2. Put a[before the 0 on line 3. Add(beforeitemsand)after on line 5.Complete the code on lines 4 and 5 so that the function returns the average of a list of integers.
On line 4, add
sum = sum + numand on line 5, addsum/ len(aList).Fix the indention in the code below so that it runs correctly. It should loop and add the current value of
sourcetosoFareach time through the loop. It should also print the value ofsoFareach time through the loop.Indent lines 4 and 5 as shown below.
Fix the code so that the code prints “[‘hihi’, 0, 0, 4]” .
On line 1, add a
,before “2”. On line 2, it should beitems[0]and a+instead of a*. On line 3, it should beitems[1].Fix 4 syntax errors in the code below. After the code executes the list
soFarshould contain the reverse of thesourcelist.Add a comma between
isandaon line 2. Add a]at the end of line 5. Add a:at the end of line 8. Change line 11 tosoFar.The code below currently prints the reverse of a list. Change it so that it prints a mirrored version of the list. It should print “[‘list’, ‘a’, ‘is’, ‘This’, ‘This’, ‘is’, ‘a’, ‘list’]”.
In the for loop make
soFarequal to[source[index]] + soFar + [source[index]].Change the following code into a function. It should take the list and return a list of the values at the even indicies.
Define a function that takes a list and then call the function and pass in the list. Print the result.
The following code creates and prints a list of even numbers. Change it and add to it so that it creates a list of all multiples of 5 from 0 to 50, inclusive.
Change
numbersto equalrange(1,51,5)and the range in the for loop to (0,len(numbers)).Change the following into a procedure. It prints a countdown from 5 to 0. Have it take the starting number for the countdown as a parameter. Print each value till it gets to 0.
Define the procedure and call it. Be sure to pass the number to start the countdown at.
Fix the errors so that the code individually adds each item from
sourcetonewList. Make the range decrement, so it starts from the end, but keepnewListin the same order assource.On line 6, the range should be
(len(source) - 1, -1, -1). Indent line 9 and switch the order the items are added.Write a function that returns the values at the odd indices in a list. The function should take the number list as a parameter. If it is passed [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for example, it should return [1, 3, 5, 7, 9].
See the function below. Be sure to create the function and call it and print the result.
Write a function that takes a list of numbers as a parameter and adds 5 to each number and returns the list.
You need to know the index of each item in the list so use the range function in the for loop.
Write a function that takes a list of numbers and returns the sum of the positive numbers in the list.
Define the function as shown below. Be sure to accumulate and return the sum. Call the function and print the result.
Write a function that takes in a list of numbers as a parameter. The function should calculate the sum of all the positive numbers in the list, the absolute value of the sum of the negative numbers, and return the average of the two sums.
Define the function as shown below.
Write a function to return the reverse of a list, but with only every other item from the original list starting at the end of the list. So, if it is passed the list [0,1,2,3,4,5] for example, it should return the list [5, 3, 1].
Define the function as shown below.
Write a procedure that takes an int as a parameter. The procedure should add every other odd number from 1 to the int parameter (inclusive) into a new list. The procedure should print the new list and the sum of the new list.
Define the procedure as below.