A Smarter Way to Learn Python / Chapter 51 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.


Post a review on Amazon for the paperback or the Kindle edition.


Email me to give me a compliment, complaint, or correction. I'll respond.


  All of my books include free interactive online exercises that make the knowledge stick.

A Smarter Way to Learn JavaScript
Shop for it at Amazon
A Smarter Way to Learn HTML and CSS
Shop for it at Amazon
A Smarter Way to Learn jQuery
Shop for it at Amazon

copyright © 2017 by Mark Myers. All rights reserved.

0,1,2,3,4,5,6,7,8,9,10,11

0

0

0

0


What is the keyword in a loop that keeps repeating as long as some condition is met? while^ *while *$
Code the first line of a while loop that keeps repeating as long as x equals 1. while x == 1:^ *while x == 1\: *$
Code the first line of a while loop that keeps repeating as long as x doesn't equal "Hanoi". while x != "Hanoi": while `x `!`= `"`Hanoi`"`:
Code the line that must precede this line. while name != "Hanoi":name = ""name = ""
As long as x doesn't equal 99, display x, then increment it by 1 using the concise way to increment. while x != 99:
  print(x)
  x += 1
^ *while x != 99:\n print\(x\)\n x \+= 1 *$
As long a one integer variable is less than another integer variable, increment the first variable by 3 using the concise way to increment. Make up the variable names. while x < y:
  x += 3
^ *while ([a-zA-Z_][a-zA-Z0-9_]*) < [a-zA-Z_][a-zA-Z0-9_]*:\n \1 \+= 3 *$
Code a function that has two parameters that are lists. As long as the first list is shorter than the second list, append an element, "empty", to the first list. Make up the names of the function and variables. def even_out(first_list, second_list):
  while len(first_list) < len(second_list):
    first_list.append("empty")
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n while len\(\1\) < len\(\2\):\n \1\.append\(•empty•\) *$
Code a function with three parameters that hold integers passed to them by the calling code. As long as the total of the first two variables is greater than the third variable, subtract half of the third variable from the first variable using the concise way to subtract. Make everything up. def reduce(x, y, z):
  while x + y > z:
    x -= z / 2
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n while \1 \+ \2 > \3:\n \1 -= \3 \/ 2 *$
Code a function that asks for the user's input. Keep asking until the user enters "You're right" Make up the name of the function, the text that prompts the user, and the variable that holds the user's input. Remember to declare the variable as an empty string in line 2. def get_validation():
  response = ""
  while response != "You're right":
    response = input("Tell me I'm right: ")
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n ([a-zA-Z_][a-zA-Z0-9_]*) = ••\n while \1 != •You◘re right•:\n \1 = input\(•.+•\) *$
As long as a variable holding an integer is less than the value of another variable, run a for loop that loops through all the elements of a list and adds each element of the list to the first variable. Use the concise approach to increment the first variable. Make everything up. while first_number < second_number:
  for number in number_list:
    first_number += number
^ *while ([a-zA-Z_][a-zA-Z0-9_]*) < [a-zA-Z_][a-zA-Z0-9_]*:\n for ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*:\n \1 \+= \2 *$
  1. Code a while loop. As long as x is less than or equal to 10, display x then add 2 to it.
  2. Click the Run button   above your code. If you've coded correctly, a column of numbers 0 2 4 6 8 10 will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
0b3460f76c
  1. As long as the counter is less than the length of list_of_names, append an element of list_of_names to second_list_of_names. Then increment the counter. (Use the counter as the index of each element as you loop through list_of_names.)
  2. Display second_list_of_names.
  3. Click the Run button   above your code. If you've coded correctly, second_list_of_names, identical to list_of_names, will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
1cb4f569e0