Computer Science > CODING SOLUTION > CS 1101 Programming Fundamentals - Term 5, 2018-2019CS 1101 - AY2019-T5_ Discussion Unit 2. With an (All)
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 ex ... amples 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 <module> #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 <module> #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]
Last updated: 10 months ago
Preview 5 out of 54 pages
Loading document previews ...
Buy this document to get the full access instantly
Instant Download Access after purchase
Buy NowInstant download
We Accept:
CS 1101 Programming Fundamentals. all terms questions and answers, practice questions with correct answers and project. the right A grade materials to pass.
By Professor Lynne 4 years ago
$11
13
Can't find what you want? Try our AI powered Search
Connected school, study & course
About the document
Uploaded On
Jun 07, 2021
Number of pages
54
Written in
All
This document has been written for:
Uploaded
Jun 07, 2021
Downloads
0
Views
245
Scholarfriends.com Online Platform by Browsegrades Inc. 651N South Broad St, Middletown DE. United States.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Scholarfriends · High quality services·