CS 1101 Programming Fundamentals - Term 5, 2018-2019CS 1101 - AY2019-T5_ Discussion Unit 2. With answers
Create your own Python code examples that demonstrate each of the following. Do not
copy examples from the book
...
CS 1101 Programming Fundamentals - Term 5, 2018-2019CS 1101 - AY2019-T5_ Discussion Unit 2. With answers
Create your own Python code examples that demonstrate each of the following. Do not
copy examples from the book or any other source. Try to be creative with your examples to
demonstrate that you invented them yourself.
Example 1: Dene a function that takes an argument. Call the function. Identify what code is
the argument and what code is the parameter.
Example 2: Call your function from Example 1 three times with dierent kinds of arguments:
a value, a variable, and an expression. Identify which kind of argument is which.
Example 3: Create a function with a local variable. Show what happens when you try to use
that variable outside the function. Explain the results.
Example 4: Create a function that takes an argument. Give the function parameter a unique
name. Show what happens when you try to use that parameter name outside the function.
21/8/2019 CS 1101 - AY2019-T5: Discussion Unit 2
https://my.uopeople.edu/mod/forum/discuss.php?d=230935 2/54
Explain the results.
Example 5: Show what happens when a variable dened outside a function has the same
name as a local variable inside a function. Explain what happens to the value of each
variable as the program runs.
For this Discussion forum, here are my examples and what I have done to complete it, I
hope I'm not mistaking anything.
#Example 1: Dene a function that takes an argument. Call the function. Identify what code
is the argument and what code is the parameter.
#Here I dene the function that will display a message per name
def congratulation(name): # congratulation is the function the (name) is the argument
print("congratulation to you!" + name)
# I will call my function, in this case, it will print the name below which is Paul & Roza are
considered as a parameter
congratulation ("Paul")
congratulation ("Roza")
#Example 2: Call your function from Example 1 three times with dierent kinds of
arguments: a value, a variable, and an expression. Identify which kind of argument is which.
# call my function with a value
congratulation ("Liza")
# call my function with a variable
variableName="Reem"
congratulation (variableName)
# call my functiion with an expression
congratulation ("Mr SRAYI Reda ")
#Example 3: Create a function with a local variable. Show what happens when you try to use
that variable outside the function. Explain the results.
21/8/2019 CS 1101 - AY2019-T5: Discussion Unit 2
https://my.uopeople.edu/mod/forum/discuss.php?d=230935 3/54
def printCountry():
country = "France"
capital = "Paris"
print (capital + " is the capital of " + country)
printCountry()
#Let's show variables country and capital outside the function printCountry ==> this will
create an error because capital and country are dened outside the function
print (capital + " is the capital of " + country) (on the attached le remove the # for
commenting in order to test )
#Traceback (most recent call last):
#File "C:\Users\rsrayi\AppData\Local\Programs\Python\Python37-32\Scripts\Discussion Forum U
nit 2.py", line 36, in
#print (capital + " is the capital of " + country)
#NameError: name 'capital' is not defined
#Example 4: Create a function that takes an argument. Give the function parameter a
unique name. Show what happens when you try to use that parameter name outside
the function. Explain the results.
def returnCarModel(model):
print ("the model is " + model)
print ("this is a good model !!!")
returnCarModel ("Honda")
#Lets call varianle model outside the function == > this will create an error
print(model) # ===> NameError: name 'model' is not defined (make sure you remove the # from m
y attached script if you want test)
#Traceback (most recent call last):
#File "C:\Users\rsrayi\AppData\Local\Programs\Python\Python37-32\Scripts\Discussion Forum U
nit 2.py", line 50, in
#print(model) # ===> NameError: name 'model' is not defined
#NameError: name 'model' is not defined
#Example 5: Show what happens when a variable dened outside a function has the same
name as a local variable inside a function. Explain what happens to the value of each
variable as the program runs.
21/8/2019 CS 1101 - AY2019-T5: Discussion Unit 2
https://my.uopeople.edu/mod/forum/discuss.php?d=230935 4/54
color = "Blue" # I defined Blue color outside the function as showing and in order to print i
t I initiate print.
print ("The color outside function is " + color)
def displaycolor ():
color = "Red"
print ("The color inside the function is " + color)
[Show More]