A Smarter Way to Learn Python / Chapter 49 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


When a variable is introduced within a function, its _______ is local. scope^ *scope *$
When a variable is introduced in the main body of code, its scope is _________. global^ *global *$
In the following code, what is the value of y? def x(num):
  a = 2
  return num * a
y = x(10)
20 2`0
In the following code, two of the variables are local. Type their names, separating the names by a comma. Type them in the order in which they appear in the code. def a(b):
  total = b + c
  return total
c = 2
num = a(10)
b,totalb,total
In the following code, what is the final value of a? def a():
  b = 2
  return b
a = 1
a = a()
2 ^ *2 *$
If a function defines the variable first_name as "George" and code outside the function defines the variable first_name as "Hamid", and the function code says, print(first_name), what name will display? Omit the quotation marks. George ^ *•*George•* *$
If a function defines the variable first_name as "George" and code outside the function defines the variable first_name as "Hamid", and the main code says, print(first_name), what name will display? Omit the quotation marks. Hamid ^ *•*Hamid•* *$
How many variables in this function can be changed by code outside the function? Enter a numeral. def calc(a):
  b = 12
  c = 404
  result = (a / b) * c
  return result
0 ^ *0 *$
How many global variables are in the following function call? (Remember, a function name is a variable.) Answer with a numeral. a(b, c, d) 4 ^ *4 *$
Below are two global variable definitions. Code a function that uses these same variable names to create local variables whose values are integers. (It's not a good idea to give local variables the same names as global variables, but this helps you understand.) Make up the name of the function and the integers. x = "Marta"
y = "Ingrid"
def create_local_variables():
  x = 1
  y = 1000
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n x = -?[0-9]+\n y = -?[0-9]+ *$
  1. Code a simple function that assigns a value to a variable.
  2. Call the function.
  3. Outside the function, deliberately break Python by trying to display that local variable. Notice what the error message says.
  4. Click the Run button   above your code. If you've coded correctly, an error message will display saying the variable is not defined. (You can click Instructions at the top of the right-hand panel to see the correct code.)
c936a7a4d2
  1. Code a simple function that assigns a value to a variable and returns it to the calling code.
  2. Outside the function, code a print statement using the function as the variable inside the parentheses.
  3. Click the Run button   above your code. If you've coded correctly, the value of the local variable will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
621c946426