You are on page 1of 80

CS1100-01 – STUDENT DISCUSSION ASSIGNMENT ANSWERS

by Nothando Mkhwanazi - Wednesday, 19 April 2023, 8:39 PM


Hi Kiyotaka!,

I was looking at your response, very straight to the point clear and all examples were answered
accordingly and correct. Very well done for your effort.

Kind regards
Thando
31 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Adam Baba - Wednesday, 19 April 2023, 8:14 PM


Hello Lou-Ahou
Well done for the work, accually I run your example one but I don't know how you do it but the
result given to me is only 9 instead of giving the parameter and argument like character will be
the parameter and the argument will be the Elizabeth, so am somehow confused where did you
got the 9 and also the Example is given NameError that the print_character is not defined. Please
I recommend you to read the textbook in the learning guide chapter 2 and 3 just to remind you
the code and how to apply them. My advise to you is be completing your assignment, to avoid
this rushing at the last date summit you should apply time management I think this strategy will
guide you well, I wish all the best success in your future career.
141 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Kiyotaka Tomii - Wednesday, 19 April 2023, 7:51 PM


My question is, is it is good practice to use the same name variable and local variable in the
script?
20 words
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Kiyotaka Tomii - Wednesday, 19 April 2023, 7:46 PM


print('Example 1')

def plus1(num):
# num is a parameter
# parameter is a name used inside a function to refer to the value passed as an argument
return num + 1

# 1 is a argument
# argument is a value provided to a function when the function is called
print(plus1(1))

# Result
# Example 1
#2

print('Example 2')
# argument is a value
print(plus1(1))

# argument is a variable
num = 2
print(plus1(num))

# argument is an expression
print(plus1(1+2))

# Result
# Example 2
#2
#3
#4

print('Example 3')
def print_three_times(str):
three_times = str * 3
print(three_times)

print_three_times('a')

# Result
# Example 3
# aaa

# print(three_times)
# NameError: name 'three_times' is not defined

# Explanation
# A NameError occurred when I try to use the variable outside the function, because the variable
defined inside the function is a local variable.
# A local variable can only used inside its function, so the error happend.

print('Example 4')
def print_four_times(unique_str):
output = unique_str * 4
print(output)

print_four_times('b')

# Result
# Example 4
# bbbb

# print(unique_str)
# NameError: name 'unique_str' is not defined

# Explanation
# A NameError occurred when I try to use the parameter outside the function, because we can't
use parameter outside the function.
# A parameter can only used inside its function, so the error happend.

print('Example 5')
output = 'output'
print(output)
print_four_times('c')
print(output)

# Result
# Example 5
# output
# cccc
# output

# Explanation
# After defining a variable named `output`, both the variable named `output` and the local
variable also named `output` exist.
# First, when I call `print(output)`, the content of the variable named `output` is the string of
`output`.
# Secondly, when I call `print_four_times('c')`, the content of the local variable named `output` is
`cccc`.
# Then, when I call `print(output)`, the content of the variable remains the string of `output`.
# So we can see that we can define and use a variable and a local variable with the same name
separately.

337 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Nothando Mkhwanazi - Wednesday, 19 April 2023, 7:29 PM


DISCUSSTION ASSIGNMENT 2

Example 1 :
def multiplying(a , b):
c=a*b
print(a, " * ", b , " = " ,c)
return c
print("MULTIPLICATION")
x = multiplying(int(input("Enter the value of a : ")),int(input("Enter the value of b : ")))

*In this function multiplying() above, at Line 1 a and b are parameters.


*At Line 6, when calling the function multiplying() two integer inputs from the user are passed.
The two input integers from the user at Line 6 are arguments.

Example 2 :

def mul(a,b):
return a*b
print(mul(3,4))
Output - 12

a) VALUES
• When passing value arguments 3 and 4 on the function mul(3,4)in Line 3, the function mul(3,4)
does computation and returns the value of 12. Therefore, integers can be multiplied by each
other.

def mul(a,b):
return a*b
x=(15)
print(mul(x,3))

Output - 45

b) VARIABLES
• In Line 4, x is a variable argument. The function mul() evaluates x as 15 and returns the value
of 45.

def mul(a,b):
return a*b
x=(4)
print(mul(((x*5)+5),4))

Output - 100

c) EXPRESSIONS
• In Line 4, x is an expression argument. The function mul() evaluates x as (4*5)+5 = 25, the
function become mul(25,4) which returns 100.

What I can say I have learnt from this example is that arguments can be in different forms, i.e.
they can be in a form of a value, variable or expression

Example 3 :
def adding(a,b,c):
d=a+b+c
print(d)
a=10
b=20
c=30
adding(d)
Output - Traceback (most recent call last):
File "C:/Users/ Softwares/Python/adding.py", line 7, in adding(d)
NameError: name 'd' is not defined. Did you mean: 'id'?

• Local variable d at Line 2 is inside the function. In Line 7, the function finds a name error
because the variable was not defined globally so when interpreter reads Line7, it reads d as a new
variable which was not defined initially that is why the function is not printing out a value. See
below, an example showing a function with globally defined variables that returns a value of 60.

def adding(a,b,c):
d=a+b+c
print(d)
a=10
b=20
c=30
adding(a,b,c)

Output - 60

Example 4 :
def areaofasquare(a,b):
print("The area of a square is")
square = a*b
return(square)
print(a,b)

Output - Traceback (most recent call last):


File "C:/Users/Softwares/Python/areaofasquare.py", line 5, in print(a,b)
NameError: name 'a' is not defined

• In Line 1, the parameters a and b are passed in the function areaofasquare(). When calling print
function at Line 5, arguments a and b are not recognized by the interpreter because they were not
defined or initialized from the first place that why line 5 give us an error.

Example 5 :
university = "UoPeople"
def institution(university):
university = "UoPeople"
print("I study Computer Science at " + university )
institution(university)

output - I study Computer Science at UoPeople


• Variables can be defined globally outside the function and locally inside the function. It does
not change the execution of a code and also does not affect the value of the variables.
496 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Adam Baba - Wednesday, 19 April 2023, 6:56 PM


Hello Hsu Mon
Good job thank you for the ideas you shared, it mean ful and helpful you clearly mentioned the
arguments and the parameters, this show that you struggle and fellow it step by step which help
you run it easy, like me to in unit 1 am somehow worry about the course after I deeply research
and fellow it effectively I experience it but it really confusing, but to be a good programmer you
have to focus on python as well. Hsu Mon keep it up this course will really help us in our future
studies wish you the best of luck.
104 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Adam Baba - Wednesday, 19 April 2023, 6:17 PM


Hi Samuel
You did a great job, I really appreciate it in all the examples you answer it shows that you
understand what are the challenges in python, I learn more things which will improve my skills
to deal with argument, function's parameter, variable.
Your question answer is:
Default
Keyword
Positional
Arbitrary keyword
Arbitrary positional
All arguments can be passed to a function because the parameters in a function call are the
functions arguments. To pass arguments to a function you have to create a new program named
functionargs and enter the code showing in listing and the code that specify in the main ()
function and the call to the adder () function, then save the functionargs and run it into
interpreter.
123 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Lou-Ahou-Elisabeth Tse - Wednesday, 19 April 2023, 5:38 PM


Example 1:
In this first example, the parameter is named Elisabeth. When the function is called, it will print
the argument, which, in this case, is the number of characters found in Elisabeth ==> 9.

def print_characters(Elisabeth):
print(len(Elisabeth))

>> print_characters ('Elisabeth')


9

Exemple 2:

- a value:
>>print_characters ('Hi everyone, my name is Elisabeth')
33

- a variable:

For this, I will need to add a variable. Let's say the letter 'a' is equal to the name Steeve and the
letter 'b' is equal to the name Sophia:

def print_characters(Elisabeth):
print(len(Elisabeth))

a= ('Steeve')
b= ('Sophia')

>>print_characters ('Steeve')
6

- an expression:

now, we can use the expression that will add the names Steeve and Sophia together.
>>print_characters (a+b)
12

Example 3:
I defined a function about the objectives of soccer. Here, the two variables are team objectives 1
and 2.

def soccer_team (objective 1, objective ):


team = (objective 1 + objeective 2)
print_soccer (team)

objective1 = ('score goals and')


objective2= (do not concede goals)

>> soccer_team (objective1, objective2)


score goals anddo not concede goals

Exemple 4:

def
178 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Maeva Dhaynaut - Wednesday, 19 April 2023, 3:51 PM


Hello Sylvester,

Thank you very much for sharing, this is a really good work. I think your work helped me
understand the difference between a parameter and an argument. From what I understood from
the book a parameter is a variable that is used in the definition of a function or method to
represent a value that will be passed to it. In your example the parameter is name. An argument,
on the other hand, is a value that is passed to a function or method when it is called. In your
example the argument is Alice.

Best wishes,

Maeva
100 words
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Maeva Dhaynaut - Wednesday, 19 April 2023, 3:37 PM


Hello Akadiri,

Really nice work, thank you for sharing.


About your question I think some best practices would be to use unique and descriptive names
for your variables and avoid using global variables. You can also use function parameters and
use namespace that can allow you to group related variables and functions together and avoid
naming conflicts. You can also use prefixes or suffixes for global variables. You should also
avoid using the same name for local and global variables, as we have seen in an example in this
week work, it can cause naming conflicts and make it difficult to understand which variable is
being referenced.

Best wishes,

Maeva
110 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Maeva Dhaynaut - Wednesday, 19 April 2023, 3:14 PM


Hello Thomas,

Great work, thank you very much for sharing so much details this is really helpful.
About your question I agree with you I was confused by the differences between parameter and
argument.
In general, a parameter is a variable that is used in the definition of a function or method to
represent a value that will be passed to it. An argument, on the other hand, is a value that is
passed to a function or method when it is called. So, when the book says "Inside the function, the
arguments are assigned to variables called parameters," it means that the values that are passed to
the function when it is called (the arguments) are assigned to the variables that are used in the
function definition to represent those values (the parameters).
Best wishes,

Maeva
137 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Hsu Mon Mon Thant - Wednesday, 19 April 2023, 1:38 PM


Hello, my instructor and classmates, 

            Glad to meet you all. Firstly, I want to apologize because I submitted my discussion
forum so late. 

 For example1, 

 >>>def welcome(name):

       print(‘Hello, ‘ + name + ‘!’)

 >>>welcome(‘Hsu Mon’)

       Hello, Hsu Mon!

 In this example, the function is welcome. The argument is ‘Hsu Mon’ and the parameter is
‘name’.

For example 2, 

 >>>def welcome(name):

     print(‘Hello, ‘ + name + ‘!’)

welcome(‘Hsu Mon’)       #Argument is a value.

name = ‘Hsu Mon’


welcome(name)         #Argument is a variable.

welcome(‘My name is, ‘ + name)       #Argument is an expression. 

The argument ‘Hsu Mon’ is a value in the first function. In the second function, the argument
‘name’ is a variable. In the third function, the argument "‘My name is, ‘ + name" is an
expression which combines with a string ‘My name is, ‘ with the  

value of the variable ‘name’.

 For example 3,

>>>def new_function():

        age = 18

        print(age)

>>>new_function()

      18

       In the first case, a function ‘new_function’ is defined with a local variable 'age'. When the
function is called, it prints out the age value of 18. The statement ‘print(age)’ needs to execute
inside the function. 

      If not, just get an error like in the below case.

>>>def new_function();

       age = 18

>>>new_function()

      print(age)

NameError: name "age" is not defined

In this case, this will output just an error. Because it is just executed outside of the function that
causes the variable ‘age’ to be not defined.

 For example 4,

>>>def shop(eggs):
       print(eggs)

>>>shop(100)

100 

In this example, a function ‘shop’ is defined with a parameter ‘eggs’. When the function is
called, an argument value ‘100’ is passed to the parameter and printed. If ‘shop(100) is executed
outside the function, it will produce an error.

 For example 5, 

>>>a = 20

>>>def my_func();

      a = 18

      print(a)       #Inside function

>>>my_func()

      print(a)       #outside function

       In this example, there are two cases. One is the inside function, and the other is the outside
function. In the inside function, when the function is called, it prints the value of a, which is 20.
The outside function has the same name as the inside function, but that doesn’t affect their
values. The inside function is defined as a local variable. 

    In Unit 1, we have learned ‘The Way of the Program, which is the foundation course of
Python programming, and for me, it’s quite easy. But in Unit 2, I struggle a little bit. Learning
the function definition looks easy, but in reality, it is difficult. Because of my illness, I can’t
concentrate on my courses. That makes my assignments late. I’m really sorry for all of you.

      Have a wonderful day, everyone.

443 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Mulwana Peter - Wednesday, 19 April 2023, 12:53 PM


Hello,
Thanks for the good work done.
To my understanding a local variable is one that is accessed within particular blocks of code
while a global variable is one that is accessible throughout the whole program.
Thanks.
37 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mulwana Peter - Wednesday, 19 April 2023, 12:50 PM


Thanks for the clarity and the preciseness of your answers. Great job.
12 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mulwana Peter - Wednesday, 19 April 2023, 12:47 PM


Hello Thomas;
Thanks for the work well done.
With regards to your question about the difference between an argument and a parameter, my
understanding was that an argument is just a variable in the parenthesis of the function header
and a parameter is a variable used in the carrying out of the function's operations in the function
body.
Thanks.
59 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Poan Chou - Wednesday, 19 April 2023, 10:39 AM


Hi Daisuke,
Good assignment,
If you define a variable both before and within a function, the local variable will overwrite the
global variable within the function's scope. However, the global variable will maintain its
original value outside the function's scope.
40 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Poan Chou - Wednesday, 19 April 2023, 10:38 AM


Hi, Good job.
There are four types of arguments in Python functions:
Positional, Keyword, Default, Variable-length arguments.
17 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Poan Chou - Wednesday, 19 April 2023, 10:37 AM


Hi Trayton,
Variables can be declared in a common scope so that they can be accessed by multiple functions.
Similarly, a function can be called in other functions.
28 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Poan Chou - Wednesday, 19 April 2023, 9:22 AM


## Example 1:
def print_func(arg):
print(arg)

#Here's an example of a function that takes an argument and prints it out:


print_func("Hello, world!")
#In this example, the argument is the string "Hello, world!" and the parameter is the variable arg
that's defined in the function definition.

## Example 2:
def print_func(arg):
print(arg)

# Calling the function with a value


print_func("Hello, world!")

# Calling the function with a variable


message = "Hello, again!"
print_func(message)

# Calling the function with an expression


print_func("The answer is " + str(42))
#In this example, the first call to the print_func() function uses a value as the argument. The
second call uses a variable, message, as the argument. And the third call uses an expression that
evaluates to a string as the argument.

## Example 3:
def my_func():
local_variable = 42

print(local_variable)
#In this example, we define a function called my_func() that has a local variable called
local_variable. When we try to print out the value of local_variable outside the function, we'll get
a NameError because the variable doesn't exist in the global scope. This is because the
local_variable is defined only within the scope of the my_func() function.

## Example 4:
def print_number(number):
print(number)

print_number(5)
print(number)
#In this example, we define a function called print_number() that takes a parameter called
number. When we try to print out the value of number outside the function, we'll get a
NameError because the variable doesn't exist in the global scope. This is because the number
variable is defined only within the scope of the print_number() function.

## Example 5:
message = "Hello"
def my_func():
message = "World"
print("Inside function:", message)

my_func()
print("Outside function:", message)
#In this example, we define a variable called message outside the my_func() function and
another variable with the same name inside the function. When we call the my_func() function, it
prints out the value of the message variable defined inside the function, which is "World". When
we print out the value of message outside the function, we get the value of the message variable
defined outside the function, which is "Hello". This is because the message variable defined
inside the function is a local variable that only exists within the function's scope and doesn't
affect the value of the message variable defined outside the function.
380 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Maeva Dhaynaut - Wednesday, 19 April 2023, 9:12 AM


Example 1:

def print_twice(maeva):
print(maeva)
print(maeva)

print_twice(‘researcher’)
researcher
researcher

In this example, print_twice is a function that takes an argument. When the function is called, it
prints the value of the parameter named maeva twice. The argument “researcher” is repeated
twice when the function is called.

Example 2:

In this example, we call the function print_twice three times with different types of arguments.

print_twice(22)
22
22

A value is passed directly to the function as an argument.

caroline = 'student at UoP'


print_twice(caroline)
student at UoP
student at UoP

A variable holds a value that is passed to the function as an argument. The name of the variable
we pass as an argument (caroline) has nothing to do with the name of the parameter (maeva).

print_twice('papillon '*2)
papillon papillon
papillon papillon

An expression is a concatenation of two string literals that is passed to the function as an


argument.

Example 3:

def call_twice(part1, part2):


feline = part1 + part2
call_twice(feline)

print(feline)
NameError: name 'feline' is not defined

In this example, we define a function print_twice that contains a local variable feline. When we
try to print feline outside of the function, we get a NameError because feline is not defined in the
global namespace. When we create a variable inside a function, it is local, which means that it
only exists inside the function.

Example 4:

def print_twice(feline):
print(feline)
print(feline)

print(feline)
NameError: name 'feline' is not defined

In this example, we define a function print_twice that takes a parameter feline. When we try to
print feline outside of the function, we get a NameError because feline is not defined in the
global namespace.

Example 5:

call = 22

def call_twice(part1, part2):


call = part1 + part2
print_twice(call)

line1 = 'feline is '


line2 = 'a cat.'
call_twice(line1, line2)
feline is a cat.
feline is a cat.

print(call)
22

In this example, we define a function call_twice that contains a local variable call. This variable
is defined outside of the function by 22 and has the same name as the local variable inside the
function. When we call call_twice, we can access the local variable within the function. When
we print call outside of the function, we get the value that was assigned to it in the global
namespace.

What are some best practices for naming variables and functions in Python, and why is it
important to follow these conventions?
399 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2


by Adam Baba - Wednesday, 19 April 2023, 9:12 AM
Example 1:
def greeting(name):

print("Hello,"+ name +"!")

greeting("Adam")

In this example, name is the parameter and "Adam" is the argument. The greeting() function
takes one parameter called name, which is used in the function body to print a greeting message.

Example 2:
def greeting(name):
print("Hello,"+ name + "!")

greeting("Adam")
greeting("Umar")
greeting("Baba")

In the first call to greeting(), "Adam" is a string literal that is used as an argument. In the second
call, "Umar" is also a string literal. In the third call, "Baba" is another string literal.

Example 3:
def my_func():
x = 42

my_func()
print(x)

in this example, x is a local variable inside the my_func() function. When the function is called,
x is defined and assigned the value 42. However, because x is a local variable, it only exist
within the function's scope. When the function finishes executing, the variable x is destroyed and
is no longer accessible. Therefore, when we try to print x outside the function, a NameError is
raised, indicating that x is not defined.

Example 4:

def double(x):
result = x *2
return result

x=5
print(double(x))
print(x)

In this example, x is defined outside the double() function and is passed as an argument to the
function. The parameter of the function is is also named x, but this does not affect the value of
the variable x defined outside the function. When we print x after calling double() , we see that
the value of x is still 5.

Example 5:

def my_func():
x = 42
print("inside the function, x is",x)

x = 10
my_func()
print("outside the function, x is",x)

In this example, x is defined both outside and inside the my_func() function. When the function
is called, a new variable named x is created with the value 42, and this variable only exists within
the function's scope. When we print x inside the function, we see that its value is 42. When we
print x outside the function, we see that its value is still 10, which is the value of the x variable
defined outside the function.

Question: How does python handle function calls with a variable number of arguments, such as
*args and **kwards?

References

1. Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea


Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

382 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mulwana Peter - Wednesday, 19 April 2023, 6:16 AM


EXAMPLE 1
def greeting(name):
print("Good morning ",name)
^^^^^^^^^^^^^^^^^^
The function purpose is to generate greetings for different persons. In this code, the argument is
the name in the parenthesis of the function head. The parameter is the name variable in the
function body which correlates with the argument in the head.

EXAMPLE 2
greeting("Simon")
Good morning Simon
^^^^^^^^^^^^^^^^
This above is the value argument.

visitor = "Katrina"
greeting(visitor)
Good morning Katrina
^^^^^^^^^^^^^^^^^
This above is the variable argument.

greeting(visitor + " Anna")


Good morning Katrina Anna
^^^^^^^^^^^^^^^^^^^^
This above is the expression argument.

EXAMPLE 3
def increase(value):
number = 10
print(value +10)

increase(13)
23
^^^^^^^^^^^^^^
The variable number is the local variable i.e. accessed only within the function.

print(number)
Traceback (most recent call last):
File "", line 1, in print(number)
NameError: name 'number' is not defined
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above code generates a name error showing that the number variable cannot be accessed
outside the function.

EXAMPLE 4
def add_next_two(value):
number = (value + 1) + (value + 2)
print(number)

add_next_two(4)
11
number = 7
print(number)
7
^^^^^^^^^^^^^^^^^^^^^^^^
The code above shows a parameter name can be used outside the function and will not interfere
with the operation of the function.

EXAMPLE 5
def previous(value):
print(value - 1)

previous(100)
99
value = 43
print(value)
43
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The code above shows that when a local variable is defined in a function, its significance is
restricted only within the function (outside the function its not existing) and for that reason a
variable can be defined outside the function with the same name without interrupting the
working of the function.

QUESTION.
With regards to the order of execution of functions in specific, are they all executed at once first
considering they can be placed anywhere in the program and still be used?
Thanks.
299 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mohammedshahid Kanyat - Tuesday, 18 April 2023, 11:25 PM


Hello Chosen Zorbari,
Greet job you did very well from your side in discussion assignment and your presentation is
nice and your explanation is clear and I have found one word f-string in your assignment. I didn't
know that use 'f-strings' to insert variables directly inside strings. I searched online about f-string
but I didn't get but I will continue that search to know what is f-string and thank you for your
post.
73 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Mahmoud Mohanna (Instructor) - Tuesday, 18 April 2023, 11:13 PM


Good answer, Samuel!
Please look at my post, just above your reply.
12 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mahmoud Mohanna (Instructor) - Tuesday, 18 April 2023, 11:12 PM


Good answer, Thomas!
Functions are not used only to organize the code. They also help in reducing the lines of the
program. Imagine a block of then lines of code and this block is used ten times in the program.
The total number of lines is 10*10 = 100 lines of code, while if we separate it in a function and
call it 10 times, the total will be = 10 + 10 = 20 lines of code.
For local and global variables usages, it's simple: if the variable is required within the function
only, we declare it locally. Otherwise, if it is used in several places in the program, we declare it
globally.
114 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mohammedshahid Kanyat - Tuesday, 18 April 2023, 11:06 PM


Hello Gilles Babou,
great job your presentation is very good and your explanation is informative and written in very
clear way I appreciate your hard work and the way you have written this program all student will
understand program and in their last you ask a question that you know about the other module,
but I will recommend you that you can search online, or looking some python documentary that
helpful to you and thank you for your post.
79 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mohammedshahid Kanyat - Tuesday, 18 April 2023, 10:44 PM


Hello Thomas Appleby,
Great job you have given your best in this discussion assignment and your coding output in clear
explanation and I see that same thing it ask the function with value variable and expression in
variable part you called the function with variable but you didn’t defined the variable in code and
on your assignment that you have highlighted the parameter with yellow and highlighted the
argument with green and I got to know some things from your coding and thank you for your
post.
87 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Hanan Sheel - Tuesday, 18 April 2023, 8:14 PM


Hi Akadiri Oluwagbemi
good job on your clear coding
9 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Hanan Sheel - Tuesday, 18 April 2023, 8:10 PM


Hi Gilles Babou
Nice work with your presentation it was clear.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Hanan Sheel - Tuesday, 18 April 2023, 8:08 PM


Hi Takeru Akasegawa
Great goob with your presentation .
9 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Sylvester Ofueu - Tuesday, 18 April 2023, 7:56 PM


Example 1:
def greet(name):
print("Hello, " + name + "!")

greet("Alice")
In this example, greet() is a function that takes one parameter, name. The argument passed when
calling the function is "Alice".

Example 2:
def greet(name):
print("Hello, " + name + "!")

greet("Alice")
greet(name="Bob")
person = "Charlie"
greet(person)

In the first call to greet(), "Alice" is a literal value passed as an argument. In the second call, the
argument is specified by name. In the third call, a variable is passed as the argument.
Example 3:
def function():
x = 42

function()
print(x)

In this example, function() declares a local variable x. The print() statement outside the function
raises a NameError because x is not defined in that scope.

Example 4:
def greet(name):
print("Hello, " + name + "!")

greet("Alice")
print(name)

In this example, greet() takes a parameter name. When the print() statement is called outside the
function, a NameError is raised because name is not defined in that scope.

Example 5:
x = "global"

def function():
x = "local"
print(x)

function()
print(x)

In this example, x is defined as "global" outside the function. Inside the function, a new variable
x is defined and assigned the value "local". When print(x) is called inside the function, it prints
"local". When print(x) is called outside the function, it prints "global", the value of the variable
defined outside the function.

Question: What is the difference between a parameter and an argument in Python?


240 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2


by Hanan Sheel - Tuesday, 18 April 2023, 7:39 PM
 :Example 1

:Input

def greet_line(name): # mark the function with parameter named

The body of the function.       print('welcome back'+name) # 

.greet_line('Shahad') # calling is the function assigns the argument

:Output

welcome backShahad

 :Example 2

:Input

:def greet_line(name)

   print('welcome back'+name)        

I call the function  three time using greet_line() with different types#

.greet_line('shahad') #a value

welcome backshahad

.line_name='noor #a variable

greet_line(line_name)

welcome backnoor

.greet_line('And nice to meet you '+line_name) # a expression

welcome backAnd nice to meet you noor

 :Example 3

:)(def FunctionA

z=10-2*5        

print('the value is: ',z)        


)(FunctionA

I defined he function when we contain with z#

the value is:  0

.when I try to make the function outside it become error#

 :Example 4

:def FunctionA(z)

print('the value of a function is: ',z)        

FunctionA(15)

the value of a function is:  15

z=1105

FunctionA(z)

the value of a function is:  1105

.x is parameter, but when we used outside the function it become not related to the parameter #

:Example 5

n=17

:)(def name_Fun

n=100        

print('the value inside',n)        

)(name_Fun

.the value inside 100 # the variable inside a function mean it only exists inside

print('the value outside',n)

..the value outside 17 # the variable outside is only focus on the variable in the outside
208 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Samuel Abraham - Tuesday, 18 April 2023, 5:13 PM


Hello! Dear Professor,
*Answers ----
1) Eventhough, we could write code in the main function, but it makes our code untidy and
uncomfortable to reuse and read. Also, as our coding becomes more complex, it will be very
difficult to organize.
2) I think using local variables is preferred when we need to access the value within a specific
code. And global variable is preferred when we need to access the value anywhere within the
program.
Best Regards,
Samuel Abraham
80 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Samuel Abraham - Tuesday, 18 April 2023, 4:31 PM


Hello! Dear Asmaa Alzoubi,
Nice work on the coding part.
* Answer --- It is very interesting question, in generally, variable is defined either local or global.
A local variable is a variable that is defined inside of a function and can be accessed within that
function only. And a global variable is a variable that is defined outside of a function and can be
accessed anywhere within the program.
Best wishes,
Samuel Abraham
74 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Samuel Abraham - Tuesday, 18 April 2023, 4:13 PM


Hello! Mr. Gilles Babou,
You did great on coding and explaining them too.
*Answer ---- To add some rather than the most known module that is math module, I know
datetime module, random module and email module. But I guess there are so many modules in
py.
Best wishes,
Samuel Abraham
51 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Samuel Abraham - Tuesday, 18 April 2023, 3:53 PM


Hello! Everyone,
Sorry for posting too late!

Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.

def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Jemal”)
Output:
Hello! Dear Jemal, where do you live?
# On creating a function which leads to an argument, first, I write the function which is address
with the parameter called name, and formatting the result which how I need to see on the output
display. Then, to pass an argument and call the function, I type the address and instead of the
name I put the name I need to ask.
So, the code “Jemal” is the argument and the code ‘name’ is the parameter.
=====================================================================
=====================================================================
==========
Example 2: Call your function from Example 1 three times with different kinds of arguments: a
value, a variable, and an expression. Identify which kind of argument is which.

# Value argument: is when the value of the argument exactly transcribed into a new variable.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Tomas”)
Output:
Hello! Dear Jemal, where do you live?
---------------------------------------------------------------------------------------------------------------------
----------
# Variable argument: is used to write a function that can accept a variable of inputs.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
character = “Nur”
address(character)
Output:
Hello! Dear Tomas, where do you live?
---------------------------------------------------------------------------------------------------------------------
----------
# An expression argument: is used to pass values as argument to a function.
Example ---
def address(name):
print (“Hello! Dear “ + name + where do you live?”)
address(“Hana”.upper ())
Output:
Hello! Dear HANA, where do you live?
=====================================================================
=====================================================================
==========
Example 3: Construct a function with a local variable. Show what happens when you try to use
that variable outside the function. Explain the results.
Example ---
def function():
x = 21
function()
print(“Output the function, x =”, x)
Output:
NameError: name 'x' is not defined
# When we try to print the value of x outside of the function, it gives ‘NameError’ because x is a
local variable and not in the global.
=====================================================================
=====================================================================
==========
Example 4: Construct 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 character(name):
print(“Hello! “ + name)
character(“John”)
print (John)
Output:
NameError: name 'John' is not defined
# When we try to call the character() function, print value of name outside of the function, it
gives NameError because name is only mentioned in local and not defined in the global.
=====================================================================
=====================================================================
==========
Example 5: Show what happens when a variable defined 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.
color = “Red”
def my_type():
color = “Blue”
print(“Inside the function, color =”, color)
my_type()
print(“Outside the function, color =”, color)
Output:
Inside the function, color = Blue
Outside the function, color = Red
# As the type of color inside and outside of the function is differ the results differ also in the
output display. And not any Error messages appear on the output, because the global variable
color has its own name outside the function as well as the local variable.
=====================================================================
=====================================================================
==========
End your discussion post with one question related to programming fundamentals learned in this
unit from which your colleagues can formulate a response or generate further discussion.

# Mention all types of arguments with examples that can be passed to a function?
605 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2


by Trayton Keyes - Tuesday, 18 April 2023, 3:44 PM
Nice job presenting your code in a clear way. To answer your question, The difference between a
local and global variable is placement inside the code, and it's availability to the rest of the
program. A global variable (usually declared at the top) and can be used by any function or
variable in the rest of the program. Local Variables are declared within a Function and are
restricted to the inside of that function. Functions can also be global or local. Functions defined
inside another function are also restricted to Local status and cannot be used outside the parent
function.
100 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Trayton Keyes - Tuesday, 18 April 2023, 3:37 PM


Hi Gilles, Nice work on the assignment. You presented the information very clearly in your
examples. I've never used Python before, so I don't know too much about the math Library. But,
If it's anything like JavaScript you can do all kinds of calculations with it like rounding up and
down, Factorials, Absolute values, and even tons of trigonometry. I'm sure there's much more
you can do with it, but that's what I know just off the top of my head. I'd recommend looking at
pythons Math library documentation if you want to learn more. You can find it just by googling
the name of the library.
107 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Trayton Keyes - Tuesday, 18 April 2023, 3:19 PM


Hey Thomas, Nice work on the coding. You did a wonderful job of making it clear what
information you were trying to get across in your descriptions. I fell like the vocabulary can
definitely be a little confusing. I found myself going back and double-checking the text book
multiple time to make sure I was getting my definitions correct on my assignment. I even used
Google and some online python docs to make sure that I wasn't getting too confused looking at
the book. I'm sure there's a good way to remember them but honestly I just kept going over it to
really drill it into my head.
108 words
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Trayton Keyes - Tuesday, 18 April 2023, 12:01 PM


#example 1
def greeting(name):
print("Hello, " + name)

greeting("Tray")

#For this example, The function is "greeting" and "name" is the parameter. The argument passed
to the function greeting, Is my name, Tray.

#example 2
def greeting(name):
print("Hello, " + name)

greeting("Tray")
greetee = "Prof"
greeting(greetee)
greeting("good morning, " + greetee + '!')
#For this example. The first shows an argument as a value being just a String. The second is an
argument as a Variable which means i can change the value of Greetee and
#the code will change. And the third is an expression, having a combination of multiple values,
Variables, or operations and a call to functions.

#example 3
def my_function():
x= 100
my_function()

print(x)
#when you try to use a local variable outside its scope, you end up with an error saying "x is not
defined".
#This is because x is only defined within the scope of the function.

#example 4
def timesTwo(num1):
num2 = 2
return num1* num2
result = timesTwo(5)
print(num1)
#In this example, when you try and print num1 outside the function we get an error "NameError"
#because Num1 is only defined within the scope of the function "timesTwo"

#example 5
var = 100

def inside_func():
var = 50
print("inside function ", var)
inside_func()

print("outside function ", var)

#In this example, The print statement inside the function assigns the value of var to the local
# variable. Outside the function, The value of var can't be changed or affected by the value set
inside the function.
# var is assigned the global value of 100 outside the function, and the local variable doesn't affect
the outside function.

My question to everyone is how would you use Variable in multiple functions? Can the same be
applied to using a function in other functions?
298 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Asmaa Alzoubi - Tuesday, 18 April 2023, 9:03 AM


Hi, Al-Kadiri, it is a wonderful work and a complete explanation of ideas. Honestly, I did not
know the answer to your question. I hope to benefit from this subject to enhance my knowledge.
Thank you

36 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2


by Asmaa Alzoubi - Tuesday, 18 April 2023, 9:01 AM
Hi Daisuke, I appreciate your trouble. Your explanation was clear, and I benefited a lot from it.
Thank you

19 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Asmaa Alzoubi - Tuesday, 18 April 2023, 9:00 AM


Hi Gilles, great presentation for your explanation and your work. I appreciate your hard work. I
strongly agree with you. It's great that we can do all kinds of mathematical calculations in the
math unit. I don't know if there are other built-in units. I hope we know the answer and benefit
each other to enhance our knowledge.

58 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Akadiri Oluwagbemi - Tuesday, 18 April 2023, 5:53 AM


Example 1:
def greet_person(name):
print(f"Hello, {name}!")
greet_person("Alice")
# Here, 'name' is the parameter and "Alice" is the argument.

Example 2:
value_arg = "John"
greet_person(42) # Value argument: 42
greet_person(value_arg) # Variable argument:
value_arg greet_person("Ada" * 2) # Expression argument: "Ada" * 2

Example 3:
def print_local_variable():
local_var = "I am a local variable."
print(local_var)
print_local_variable()
# print(local_var) # Uncommenting this line will cause an error.
# Explanation:
# The local_var is defined inside the function and is not accessible outside the function scope.
# Trying to access it outside the function will result in a NameError.

Example 4:
def print_unique_parameter(unique_param):
print(f"The unique parameter is: {unique_param}")

print_unique_parameter("Hello, World!")
# print(unique_param) # Uncommenting this line will cause an error.
# Explanation: # The 'unique_param' is a parameter of the function, and its scope is limited to the
function.
# Trying to access it outside the function will result in a NameError.

Example 5:
global_var = "I am a global variable."

def print_conflicting_variables():
global_var = "I am a local variable."

print(global_var) print(global_var) # Before calling the function print_conflicting_variables()


print(global_var) # After calling the function
# Explanation:
# The global_var outside the function retains its value because the function creates a local
variable
# with the same name, which does not affect the global variable's value. The function prints the
local
# variable's value, while the global variable's value is printed before and after the function call.

Question for further discussion: What are some best practices for managing variable scope and
avoiding naming conflicts between global and local variables in your code?
258 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 2:47 AM


Hi, Chosen
Great job! Your examples are simple and so impressive that they really interest me while reading
your post.

Additionally, I have a fresh insight from your answer in Example4, which describes an “f-string”
format like “print(f"Life is food and {python}")”. It gave me a helpful opportunity to dive into
its functionality.
53 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 2:02 AM


Hi Thomas,
Great job! And I am really impressed by your well-structured expressions for the assignments. It
looks very simple and gives me a clear understanding of your insights and ideas.

>QUESTION:
Yes, I was confused and it was a bit difficult to understand the difference between a parameter
and an argument. As Gilles kindly mentioned in the reply to your post, I have the same opinion
and understanding for this difference now.
73 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 1:50 AM


Hi, Takeru

Great job! Your examples of codes are all simple, easy to understand and additionally really fun
to read because words which are used as elements of codes are friendly.

Especially, I am really grateful to you because your result and insight for Example5 made me
notice my misunderstanding, in which I had thought that a variable defined before a function is
called is to be overwritten by a variable which had the same name as it when it was called inside
the function.
85 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 1:35 AM


Hi, Asmaa

Nice job and I can find clear understandings of the meanings of your codes by way of the
appropriate comments you put after each code.
But, I found some points that did not fit the requirements for this assignment. For instance,
example 3 requires us to construct a program and to examine what happens when we intend to
use a variable which is defined inside the program from outside a function.

In the case of your example3, “y=("book")” seems to represent a definition of a variable internal
of a function. If you use this variable after the function has been executed, it can meet the
requirement of this assignment.

Let me apologize if you do not intend to express such context.


123 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 1:02 AM


[A question for discussions]
Are there any valuable tools which can manage variables defined in a program?
When a program gets longer, it can become more difficult for a programmer to remember a
meaning for each variable and to avoid duplications. In this case, those useful tools can help a
programmer construct a longer program more fluently.
57 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Daisuke Ikari - Tuesday, 18 April 2023, 12:45 AM


Example 1:
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")

ex1(1, 5)

[Output]
6
You find the addition of 1 and 5!

[Explanation]
In this case, the arguments are defined to be input in the statement of the definition of the
function “ex1”. There are two arguments to be passed by an executor of this code. Therefore,
you can find two arguments passed in the execution of the code “ex1(1, 5)”.

The parameters are defined as “arg1” and “arg2” as well. The arguments passed when this
function is called are assigned to these two parameters.

Example 2:
(1)for a value
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")

ex1(3, 1000) #Each value is assigned to each argument

[Output]
1003
You find the addition of 3 and 1000!

(2)for a variable
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")

variable1 = 505 #arg1 is assigned to variable1


variable2 = 707 #arg2 is assigned to variable2

ex1(variable1, variable2)

[Output]
1212
You find the addition of 505 and 707!

(3)for an expression
[Code]
def ex1(arg1, arg2):
result1 = arg1 + arg2
print(result1)
print("You find the addition of " + str(arg1) + " and " + str(arg2) + "!")

n = 33

ex1(150, n*3+1) #”n*3+1” is an expression assigned as an argument

[Output]
250
You find the addition of 150 and 100!

Example 3:
[Code]
import math

def circle(arg1): #arg1: radius of the circle


diameter = arg1 * 2
area = math.pi * arg1 ** 2
print("The diameter of the circle is " + str(diameter))
print("The are of the circle is " + str(area))

circle(10)

print(diameter)
print(area)

[Output]
The diameter of the circle is 20
The are of the circle is 314.1592653589793
Traceback (most recent call last):
File "/home/daisukeikari/df.py", line 11, in print(diameter)
NameError: name 'diameter' is not defined

[Explanation]
The valuable “diameter” is defined inside the function “circle”, which is recognised as a local
variable. A local variable becomes invalid after the execution of the function which involves the
local variable. Hence, you cannot use it outside the function.

Example 4:
[Code]
def triangle(base, height): #Two arguments are assigned to each parameter

area = base * height / 2


print("The area of the triangle is " + str(area) +"!")

triangle(10, 5)

print(base)
print(height)

[Output]
The area of the triangle is 25.0!
Traceback (most recent call last):
File "/home/daisukeikari/df.py", line 7, in print(base)
NameError: name 'base' is not defined

[Explanation]
In this case, two arguments defined in the statement of the definition of the function are assigned
to each parameter, base and height. These parameters are valid until the function has been
executed, which means they are invalid after it and you cannot use them outside of the function.

Example 5:
[Code]
area = "An area of a triangle is defined as a half of multiplication of base and height!"

print(area) #A debugger of the content of a variable “area”

def triangle(base, height):


area = base * height / 2

print(area) #A debugger of the content of a variable “area”


print("The are of the triangle is " + str(area) +"!")
triangle(10, 5)

[Output]
An area of a triangle is defined as a half of multiplication of base and height!
25.0
The area of the triangle is 25.0!
>>>

[Explanation]
In this case, when I define the variable “area” before the definition as a local variable, the output
indicates that the first definition for “area” is overwritten by the second one, a local variable.
623 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Thomas Appleby - Monday, 17 April 2023, 11:27 PM


Dear Professor,
In regards to the first question, I believe that functions allow us to organize the program better.
We can look at each function and see its purpose in the program. It also makes it easier to debug
and troubleshoot any problems since we can work on each function.
The answer to the second question is a little bit relative. Variables that need to be called often
and used consistently throughout the program should be global variables. However, variables
that could change from function to function should be local variables.
91 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mahmoud Mohanna (Instructor) - Monday, 17 April 2023, 10:27 PM


Two reflection questions:
Why you need to use functions? Why not writing the code in the main function calling an
external function using parameters?
When it is preferred to use local variables? And when it is preferred to use global variables?
41 words
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:04 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:03 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:03 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:02 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:01 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 7:00 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 6:59 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Monday, 17 April 2023, 6:56 PM


Great job on clearly presenting your code and having clear explanation.
11 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Thomas Appleby - Monday, 17 April 2023, 12:06 AM


Hi Takeru,
Great coding. I enjoyed the creativity that went into it. It was interesting to see the different
programs you created. They are all great ways to describe someone’s favorite restaurant or store.
This could be used to create a questionnaire for someone. That would be the best application. I
also noticed that you didn’t provide a question for our peers. It is a good idea to read until the
end of the assignment to ensure that everything is complete. Other than that, you did a great job.
I look forward to reading more of your code in the future.
101 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Gilles Babou - Sunday, 16 April 2023, 11:35 PM


Hello MohammedShahid,
Great job completing the assignment. For the example 1 you created a function that takes in an
argument n and return the cube of n. You used the code 'n*n*n' to return the cube. I believe that
you can get the same result if you do 'return n**3'. That is using the exponentiation operator '**'.
Your example 2 is very well done and i like the fact that you created a variable outside the
function. I did the same thing.
82 words

PermalinkShow parentReply
Re: Discussion Assignment - Unit 2

by Thomas Appleby - Sunday, 16 April 2023, 11:08 PM


Hello Mohammedshahid,
Nice work with the coding. You organized your activity well. One suggestion for the first
example is to optimize the code is to use the exponent function rather than multiplying by three.
For example, you could write (n**3) as the code. The same thing could be done to example 2, 3
and 4. I also noticed that you didn’t provide a question for our peers. It is a good idea to read
until the end of the assignment to make sure everything is complete. Your explanations were
clear and easy to understand. Keep working hard and you will always do great.
103 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Asmaa Alzoubi - Sunday, 16 April 2023, 2:21 PM


EXAMPLE(1):

def say_hi(name):

    print("Hello" +name)

say_hi("Asmaa")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa
Process finished with exit code 0

--------------------------------------------------------------------------------

EXAMPLE(2):

def say_hi(name):#(name) is a value

    print("Hello " + name)

    X=("Asmaa")#(X=("Asmaa") ) is an expression

    say_hi(X) #(X) is variable

  

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa

Process finished with exit code 0

---------------------------------------------------------------------------------------------------

EXAMPLE(3):

#1

def my_room(things):

    y=("book")#variable entered into the function 

    print("the things at my room " + y)

my_room("y")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

the things at my room book

Process finished with exit code 0

#2

def my_room(things):

y=("book")#The variable exits the function

    print("the things at my room " + y)

my_room("y")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 2

    y=("book")

  ^

IndentationError: expected an indented block after function definition on line 1

Process finished with exit code 1

-------------------------------------------------------------

EXAMPLE(4):

#(1)

def food_menu(day):

    print("my dish will be chicken" + day)


food_menu("monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chickenmonday

Process finished with exit code 0

# (2)

def food_menu(day):

    print("my dish will be chicken" + day)

day #use that parameter name outside the function

food_menu("monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 3

    day

       ^

IndentationError: unindent does not match any outer indentation level

Process finished with exit code 1

--------------------------------------------------------------------

EXAMPLE (5):

#(1)

def food_menu(day):
    T=("and juice")# I made a local variable

    print("my dish will be chicken" + day + T )

food_menu(" monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

Process finished with exit code 0

#(2)

def food_menu(day):

    T=("and juice")

    print("my dish will be chicken" + day + T )

food_menu(" monday")

T=("toy") #The variable outside the function has the same name but a different value

print(T)

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

toy
Process finished with exit code 0

-------------------------------------------------------------

###

My question is my friends what is the difference between Local variable and Global variable?

295 words

 PYTHON 2.rtf
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Asmaa Alzoubi - Sunday, 16 April 2023, 2:21 PM


EXAMPLE(1):

def say_hi(name):

    print("Hello" +name)

say_hi("Asmaa")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa

Process finished with exit code 0

--------------------------------------------------------------------------------
EXAMPLE(2):

def say_hi(name):#(name) is a value

    print("Hello " + name)

    X=("Asmaa")#(X=("Asmaa") ) is an expression

    say_hi(X) #(X) is variable

  

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa

Process finished with exit code 0

---------------------------------------------------------------------------------------------------

EXAMPLE(3):

#1

def my_room(things):

    y=("book")#variable entered into the function 

    print("the things at my room " + y)

my_room("y")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 
the things at my room book

Process finished with exit code 0

#2

def my_room(things):

y=("book")#The variable exits the function

    print("the things at my room " + y)

my_room("y")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 2

    y=("book")

  ^

IndentationError: expected an indented block after function definition on line 1

Process finished with exit code 1

-------------------------------------------------------------

EXAMPLE(4):

#(1)

def food_menu(day):

    print("my dish will be chicken" + day)

food_menu("monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chickenmonday

Process finished with exit code 0

# (2)

def food_menu(day):

    print("my dish will be chicken" + day)

day #use that parameter name outside the function

food_menu("monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 3

    day

       ^

IndentationError: unindent does not match any outer indentation level

Process finished with exit code 1

--------------------------------------------------------------------

EXAMPLE (5):

#(1)

def food_menu(day):

    T=("and juice")# I made a local variable

    print("my dish will be chicken" + day + T )


food_menu(" monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

Process finished with exit code 0

#(2)

def food_menu(day):

    T=("and juice")

    print("my dish will be chicken" + day + T )

food_menu(" monday")

T=("toy") #The variable outside the function has the same name but a different value

print(T)

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

toy

Process finished with exit code 0

-------------------------------------------------------------
###

My question is my friends what is the difference between Local variable and Global variable?

295 words

 PYTHON 2.rtf
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Asmaa Alzoubi - Sunday, 16 April 2023, 2:21 PM


EXAMPLE(1):

def say_hi(name):

    print("Hello" +name)

say_hi("Asmaa")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa

Process finished with exit code 0

--------------------------------------------------------------------------------

EXAMPLE(2):

def say_hi(name):#(name) is a value


    print("Hello " + name)

    X=("Asmaa")#(X=("Asmaa") ) is an expression

    say_hi(X) #(X) is variable

  

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

HelloAsmaa

Process finished with exit code 0

---------------------------------------------------------------------------------------------------

EXAMPLE(3):

#1

def my_room(things):

    y=("book")#variable entered into the function 

    print("the things at my room " + y)

my_room("y")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

the things at my room book


Process finished with exit code 0

#2

def my_room(things):

y=("book")#The variable exits the function

    print("the things at my room " + y)

my_room("y")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 2

    y=("book")

  ^

IndentationError: expected an indented block after function definition on line 1

Process finished with exit code 1

-------------------------------------------------------------

EXAMPLE(4):

#(1)

def food_menu(day):

    print("my dish will be chicken" + day)

food_menu("monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 
my dish will be chickenmonday

Process finished with exit code 0

# (2)

def food_menu(day):

    print("my dish will be chicken" + day)

day #use that parameter name outside the function

food_menu("monday")

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

  File "C:\Users\D\PycharmProjects\pythonProject2\tryme.py", line 3

    day

       ^

IndentationError: unindent does not match any outer indentation level

Process finished with exit code 1

--------------------------------------------------------------------

EXAMPLE (5):

#(1)

def food_menu(day):

    T=("and juice")# I made a local variable

    print("my dish will be chicken" + day + T )

food_menu(" monday")
C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

Process finished with exit code 0

#(2)

def food_menu(day):

    T=("and juice")

    print("my dish will be chicken" + day + T )

food_menu(" monday")

T=("toy") #The variable outside the function has the same name but a different value

print(T)

C:\Users\D\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:\Users\D\
PycharmProjects\pythonProject2\tryme.py 

my dish will be chicken mondayand juice

toy

Process finished with exit code 0

-------------------------------------------------------------

###

My question is my friends what is the difference between Local variable and Global variable?
295 words

 PYTHON 2.rtf
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Gilles Babou - Sunday, 16 April 2023, 9:03 AM


Hi chosen,
Great work on completing all the tasks. I actually learned something I didn't know by reading
your assignment. I didn't know that we could use 'f-strings' to insert variables directly inside
strings. I think that is a great feature as it can save us a lot of time. I read the chapter 3 of the
book but i did not see that f-string explained. I wonder how did you find out about that feature.
Did you use IDLE or do you use another text editor. My favorite IDE right now is vs code. I like
it because it has a lot of features.
105 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Gilles Babou - Sunday, 16 April 2023, 8:39 AM


Hi Thomas,
Great job on clearly presenting your code and having clear explanation. I notice that for the
example 2 where it asks to call the function with a value, variable and expression for the variable
part you called the function with a variable 'apple' but you didn't define the variable in your code.
That would definetely give an error. For that same task, i first create a variable in the code and
assigned it a string. I then called the function with that variable and I didn't get an error because
the variable existed.

Yes in the beginning I was a little bit confused about what is an argument and what is a
parameter. But now I understand that the parameter is the name you put inside the parenthesis of
the function definition; and an argument is the code you put inside the parenthesis when you call
the function.
150 words
PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Gilles Babou - Sunday, 16 April 2023, 5:47 AM


Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.

Code:

def sayHello(name):

    print('Hi ' + name)

sayHello(gilles)

This code defines a function called ‘sayHello’. The function has a parameter called ‘name’.  The
body of the function has a print statement that will print ‘Hi <name>’ when the function is
called.

The second statement of the code call the sayHello function with the argument ‘gilles’

Output:

Traceback (most recent call last):

  File "/home/gilles/unit2.py", line 4, in <module>

    sayHello(gilles)

Hi gilles

When the code is ran it prints ‘Hi gilles’. ‘gilles’ is the argument that was passed when we called
the function

 
Example 2: Call your function from Example 1 three times with different kinds of arguments: a
value, a variable, and an expression. Identify which kind of argument is which.

Code:

def sayHello(name):

    print('Hi ' + name)

user = 'john'

# in the code bellow we are calling the function with the value 'gilles'

sayHello('gilles')

# in the code bellow we are calling the function with the variable 'user'

sayHello(user)

# in the code bellow we are calling the function with the expression "user + ' doe'"

sayHello(user + ' doe')

ouput:

Hi gilles
Hi john
Hi john doe
 

The first output print out the value ‘gilles’. The second output is the ‘user’ variable. The third
output is the expression ‘user + ‘ doe’’ which prints out john doe.

 
Example 3: Construct a function with a local variable. Show what happens when you try to use
that variable outside the function. Explain the results.

Code:

def localVariable():

    var = 'i am a local variable'

    print(var)

   

print(var)

The above code define a function called ‘localVariable’. Inside the function we assign the value
‘i am a local variable’ to a variable called ‘var’. Then we have a print statement with ‘var’ as
argument.

Then outside the function we have a print statement using ‘var’ as the argument.

Output:

Traceback (most recent call last): File "/home/gilles/unit2.py", line 5, in <module>


print(var)NameError: name 'var' is not defined

Because I define the variable ‘var’ inside the function ‘’ocalVariable' that variable is only
accessible inside that function. So in the code when I try to print it with the ‘print(var)’ code, that
will create an error because i’m using ‘var’ outside of the function.

Example 4: Construct 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 localParam(foo):
    print(foo)

   

print(foo)

In the above code we defined a function called ‘localParam’ that takes a parameter called ‘foo’.
Inside the function we have a print statement to print ‘foo’

Ouput:

Traceback (most recent call last): File "/home/gilles/unit2.py", line 4, in <module>


print(foo)NameError: name 'foo' is not defined

The parameter of a function is considered a local variable of that function in python. So we can
not use a parameter of a function outside of that function. In my code I try to use the parameter
outside of the function with the print statement; so we get an error telling us that ‘foo’ is not
defined.

Example 5: Show what happens when a variable defined 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.

Code:

x = 10  # global variable

def my_function():

    x = 20  # local variable

    print("Inside the function, x =", x)

 
my_function()

print("Outside the function, x =", x)

In the above code we defined a variable called ‘x’ outside the function and we also defined a
variable with the same name inside the function called ‘myfunction’. When my function is called
we will print the value of x that is inside my function. Outside the function we use the print
statement to also print the value of x.

Output:

Inside the function, x = 20


Outside the function, x = 10
 

Inside the function, the local variable x is assigned the value 20, and when we print it, we get 20.
Outside the function, the global variable x still has the value 10, which is what we see when we
print it.

Programming question

I really like the math module and the fact that we can do all kinds of math calculation with it. I
am eager to know what other built in modules exist in python. Do you know about any other
modules?

752 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Takeru Akasegawa - Sunday, 16 April 2023, 1:09 AM


Ex.1

def print_a(cafe):

print("My favorite place is "+cafe)

>>> print_a("Starbucks")

My favorite place is Starbucks

In this case, `cafe` is the parameter because it’s a variable for argument. When you want to use
this function, you have to replace `cafe` to other words. Argument is “Starbucks” in this function
that is assigned to the parameter `cafe`.

Ex.2

>>>print_a(“McCafe”)

My favorite place is McCafe

Firstly using a value as an argument, `McCafe` is a value in this case.

>>> coffee_shop = "Doutor"

>>> print_a(coffee_shop)

My favorite place is Doutor

Secondly, `coffee_shop` is a variable that equals `Doutor`. I use `coffee_shop` for variable

 
>>> print_a("Tully's "+"coffee")

My favorite place is Tully's coffee

Finally, the expression of this case is `“Tully’s “+”coffee”`. As a result, this evaluates to
`”Tully’s coffee”` and added into the function.

Ex.3

def player_info(name,club):

player=name+club

print(player+" scored goal")

>>> name1="Trent"

>>> club1=" Liverpool"

>>> player_info(name1,club1)

Trent Liverpool scored goal

>>> print(player)Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'player' is not defined

I define `player` for local variable in this function. When I try to use this variable outside the
function, I got the NameError which means this variable is not defined. This is because once you
finished your function, your local variable are destroyed so you can use it anymore.

 
Ex.4

def store(name):

print("I went to "+name+" yesterday")

>>> store("KFC")

I went to KFC yesterday

>>> print(name)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'name' is not defined

The parameter of this function is `name`. When I try to use `name` outside the function, it


doesn’t work well and Name error has happened. The reason for this is because this parameter
can only use in the `store()` function.

Ex.5

size='huge'

def normal():

size='narrow'

print("My town is ",size)

>>> normal()
My town is narrow

>>> size

'huge'

When I defined same variable inside and outside of the function and I called normal(), local
variable which is `narrow` was applied to the results. This results indicate that local variable is
the priority when the program runs. And then, the variable `huge` which is outside of the
function became effective when the function `normal()` completes execution.

Overall, I learned that making own function is convenient and effective to run code and at the
same time, you have to be careful how to use it. Also, I understand that function is something
that given one value, gives another value and it’s not a summary of the process.

424 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Mohammedshahid Kanyat - Saturday, 15 April 2023, 6:01 PM


Example 1: Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
def cube(n): #here n is a parameter
return n*n*n
print (cube (10)) #here 10 is a argument
output: - 1000
Explanation: - Here I have created a cube function that uses n as the parameter. Here 10 is the
argument passed to the cube function during the function call.

Example 2: Call your function from Example 1 three times with different kinds of arguments: a
value, a variable, and an expression. Identify which kind of argument is which.
def cube(n): #here n is a parameter
return n*n*n
print (cube (10)) #here 10 is a value
m=12
print (cube(m)) #here m is a variable
print(cube(m+10-5)) #here (m+10-5) is a expression
output: -1000
1728
4913
Explanation: - Using the cube function created in e.g., I have created a variable m that contains
the value 12 & then and then I called a function in that function I create an expression (m+10-5).

Example 3: Construct a function with a local variable. Show what happens when you try to use
that variable outside the function. Explain the results.
n=5
def cube(n):
ans=n*n*n #here ans is a local variable
print(ans)
output: -Traceback (most recent call last):
File "C:/Users/mskan/OneDrive/Desktop/p1.py", line 4, in print(ans)
NameError: name 'ans' is not defined. Did you mean: 'abs'?
Explanation: - The cube function in this e.g., contains a local variable n. When we try to access
this variable outside the cube function, Python returns the Name error as it is unable to identify
the local variable n. The range of local variable n is limited only inside the cube function.

Example 4: Construct 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 cube(n):
return n*n*n
print(cube (2)) #8
print(n)
output: -8
Traceback (most recent call last):
File "C:/Users/mskan/OneDrive/Desktop/p1.py", line 4, in print(n)
NameError: name 'n' is not defined
Explanation: - Here I created a function named cube and gave the unique name and after that I
got the output show me the NameError: name 'n' is not defined because variable n is not exist
outside cube ().

Example 5: Show what happens when a variable defined 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.
var=5 #here var is an outer variable
def sample():
var=10 # here var is a inner variable
print(var) #this statement print the value of inner variable
print(var) #this statement print the value of outer variable
output: -5
Explanation: - The variable var defined outside the sample function is a global variable. The
variable var defined inside the sample function is a local variable. When the code is executed,
python will print return the value of the global variable var=5.
497 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Thomas Appleby - Saturday, 15 April 2023, 1:13 PM


Hey Chosen,
Great work! I just wanted to add a few comments. I was not sure what EPL was at first, so
maybe a comment could be added to better what it is in the code to explain it. The rest of the
functions looked great. I was also curious what the purpose of the function in Example 4. Is it to
say a motivational phrase about food and something else? One more thing to note is that you
missed the last part of the assignment. It states that there needs to be a question at the end of your
post to generate further discussion. It is good to read the assignments until the end.
-Thomas
116 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Chosen Zorbari - Friday, 14 April 2023, 8:47 PM


Example 1: Define a function that takes an argument. Call the function. Identify what code is
the argument and what code is the parameter.
def epl_player(wingspan,height):
print(f"To make it into the EPL, a player must possess a minimum wingspan of
{wingspan} and a height of {height}.")
epl_player("6ft11","6ft7")
Output
To make it into the EPL, a player must possess a minimum wingspan of 6ft11 and a height of
6ft7.
Explanation
epl_player is the function,
wingspan and height are the parameters.
The arguments are the actual values of the function, therefore 6ft11 and 6ft7 are the
arguments of the function.
Example 2: Call your function 3 times with different kinds of arguments a value, a variable,
an expression. Identify which kind of argument is which.
For a value
epl_player("6ft11","6ft7")
6ft11 and 6ft7 are values
For a variable.
x=('6ft11')
y=('6ft7')
epl_player(x,y)
For an Expression
epl_player(str(6)+'ft'+str(11),str(6)+'ft'+str(7))
Output for all Arguments
To make it into the EPL, a player must possess a minimum wingspan of 6ft11 and a height of
6ft7.
Example 3: Create a function with a local variable. Show what happens if you try to use that
variable outside the function. Explain the results.
def comp_science(programming,coding):
math=programmming+coding
comp_science(math)
space_bar='physics'
space2_bar='chemistry'
print(math)
Result and Explanation
An error message is printed because the variable is not defined outside the function i.e it is a
local variable. A local variable is only available in the block(function) it is defined in, it is not
accessible outside that block.
Example 4: Create a function that takes an argument. Give the function parameter a unique
name. Show what happens when you try to use the parameter outside the function. Explain
results.
def trust_mandi(python):
print(f"Life is food and {python}")
trust_mandi("uopeople")
print(python)
Output
Life is food and uopeople
Traceback (most recent call last):
File "C:\Users\wcdub\AppData\Local\Programs\Python\Python39\muedzi.py", line 5, in
print(python)
NameError: name 'python' is not defined
Result and Explanation
An error message is printed because python looks for a global variable named python but
does not find it. Therefore a parameter name cannot be used outside the function, if it is not
defined outside that function. The life of a parameter does not exist if the function it is linked
to is terminated
Example 5: Show what happens when a variable defined outside a function has the same
name as a variable inside a function. Explain what happens to the value of each variable as
the program runs
def my_func():
x=25 # inside variavble
print(x)
x=25 # outside variable
print(x)
print(my_func())
Output
25
25
None
Reason
The variable x inside my_func function does not affect the x variable outside the function
because although they are similar they are independent of each other, and the python
interpreter executes statements one at a time. Therefore any change of inner variable will not
change outer variable and vice versa.

Resources

David J. Eck. (2019). Introduction to Programming Using Java. Retrieved


from http://math.hws.edu/javanotes/
477 words

PermalinkShow parentReply

Re: Discussion Assignment - Unit 2

by Thomas Appleby - Friday, 14 April 2023, 12:55 AM


Discussion Assignment Unit 2

Example 1: Define a function that takes an argument. Call the function. Identify what code
is the argument and what code is the parameter.

Code:

def first(thomas):

print(thomas + " Last")

Output:
>>> first("bob")

bob last

The highlighted yellow part is the parameter.

The highlighted green part is the argument.

Explanation:

The function creates the parameter, which is called by the argument when it is run.

Example 2: Call your function from Example 1 three times with different kinds of
arguments: a value, a variable, and an expression. Identify which kind of argument is which. 

Code:

def first(thomas):

print(thomas + " Last")

Output for Value:

>>> first(“bob”)

bob last

Explanation:

It combines the value “bob” with the function.

Output for Variable:

>>> first(apple)

Traceback (most recent call last):

File "<interactive input>", line 1, in <module>

NameError: name 'apple' is not defined


Explanation:

Give an error because there is no function defined for the variable apple.

Output for Expression:

>>> first("bob" + 20)

Traceback (most recent call last):

File "<interactive input>", line 1, in <module>

TypeError: can only concatenate str (not "int") to str

Explanation:

Gives an error because integers cannot be added to words as a calculation.

Example 3: Construct a function with a local variable. Show what happens when you try to
use that variable outside the function. Explain the results.
Code:

def first(thomas):

print(thomas + " Last")

Output:

>>> print(thomas)

Traceback (most recent call last):

File "<interactive input>", line 1, in <module>

NameError: name 'thomas' is not defined

Explanation:

Variables are local. They can only be used inside the created function. To use them outside the
function, I would need to define that variable outside the function. Here is an example that does
not give an error, since it is defined outside the function.

Code:
def first(thomas):

print(thomas + " Last")

thomas = 20

Output:

>>> print(thomas)

20

Example 4: Construct 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.

Code:

def first(thomas):

print(thomas + " Last")

Output:

>>> print(first)

<function first at 0x00000291E2229080>

Explanation:

The function created a function object. So when the name of this parameter is called, it prints the
object code.

Example 5: Show what happens when a variable defined 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.

Code:

def first(thomas):

print(thomas + " Last")

thomas = 20
Outputs:

>>> print(thomas)

20

>>> first("bob")

bob last

Explanation:

This example is the solution I gave to example 3 to solve the issue of the error. I ran two outputs
to test the inside and the outside variable. It simply inputs the variable it will call the outside
variable. If I input the function with the variable, it will call the variable with the rest of the
function.

QUESTION:

Did anyone else first confuse parameter with argument, since they are so
similar, especially with this sentence from the book “Inside the function,
the arguments are assigned to variables called parameters”?
 

You might also like