Difference between revisions of "Scope exercises"

From WLCS
(Created page with "''Variable scope'' --Exercise A-- Copy the following code: <syntaxhighlight lang="Python"> # obtain last name from user def getLastName(): last_name=raw_input("What's your ...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
''Variable scope''
+
==Exercise A==
 
+
Fix the following buggy code:
--Exercise A--
 
Copy the following code:
 
  
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
Line 32: Line 30:
 
# Modify the combineName function so the suffix is added to full_name
 
# Modify the combineName function so the suffix is added to full_name
  
--Exercise B--
+
==Exercise B==
* Create a new program  
+
Create a new program:
 
* Define two variables, 'x' and 'screen_width'
 
* Define two variables, 'x' and 'screen_width'
 
* Set screen_width to 800
 
* Set screen_width to 800
* Ask the user for 'x'
+
* Ask the user for a value for 'x'
 
+
* Write a function that performs screen wrapping(as in 'Robot'): <br/> If the input is greater than screen width, set it to  0 <br/> If the input is less than 0 then set it to the screen width
* Write a function that performs the screen wrapping you created in Robot :  
+
* Add code to print 'x' before and after the function is called
If the input is greater than screen width, set it to  0
+
* Test the code with several sample values - make sure it works.
If the input is less than 0 then set it to the screen width
 
 
 
* Add code to print x before and after the function is called
 
* Test the code with several sample values
 
 
* Extend the wrapping function so that it also works with the y-coordinate  
 
* Extend the wrapping function so that it also works with the y-coordinate  
* How can we return two variables (x and y co-ordinates)?
+
** Don't create a second function. Instead work out how to return two variables.

Latest revision as of 22:07, 27 February 2011

Exercise A

Fix the following buggy code:

# obtain last name from user
def getLastName():
    last_name=raw_input("What's your last name?")

# create a full name from the first and last names
def combineName():
    full_name=first_name+" "+last_name
    print_name()
    
#Print user's name
def print_name():
    print "User's full name:"+full_name
    
first_name=raw_input("What's your first name?")
getLastName()
combineName()
  1. What is the scope of the following variables
    1. last_name
    2. first_name
    3. full_name?
  2. Ensure last_name is transferred from getLastName() to the default scope
  3. What argument needs to be passed to print_name()?
  4. Add a new function that asks "Are you (j)junior, (s)senior or (n) neither?", and determines the correct suffix ("Jr.", "Sr.")
  5. Modify the combineName function so the suffix is added to full_name

Exercise B

Create a new program:

  • Define two variables, 'x' and 'screen_width'
  • Set screen_width to 800
  • Ask the user for a value for 'x'
  • Write a function that performs screen wrapping(as in 'Robot'):
    If the input is greater than screen width, set it to 0
    If the input is less than 0 then set it to the screen width
  • Add code to print 'x' before and after the function is called
  • Test the code with several sample values - make sure it works.
  • Extend the wrapping function so that it also works with the y-coordinate
    • Don't create a second function. Instead work out how to return two variables.