In: Computer Science
Write a statement that imports
3.a. Only getMyName function
3.b all other functions from the module
Requirement: you will be writing two functions
Submission: Turn in this document with answers and two python files for question # 6
Please find the answers below.
1) What is the difference between local and global variable?
A global variable is a variable that can be accessible globally.
A local variable aredeclared within a fuction block. The scope of this variable is limited to the current function block only.
Please find the example below,
q = "I am a global variable" # global variable
def f():
p = "I am a local variable" # local variable
return p
print(f())
print (q)
OUTPUT :
2) Functions are also called __method__________.
3) In python, to import functions from modules we use the keyword ‘import’, assume that you have a module named “mySelf” the mySelf module has “getMyName”, “getMyage” , and many more functions.
3.a. Only getMyName function.
The statement will be from mySelf import getMyName
3.b all other functions from the module.
The statement will be import mySelf
4. How do you differentiate a variable from a function?
In python we used variable to store a value. For example , in the above python code , q and p are two variables . Whereas, in order to define functions in python we can use the def keyword . A function can hold multiple variables and those variables are used to execute the function block. In the above example : f() is a function which is defined as def f().
5. Write a function named times_ten. The function should accept an argument and display the product of its argument multiplied by a negative ten (-10)
def times_ten(n):
#printing the product of the given number with -10
print("The product is {}".format(-10*n))
#Calling the function
times_ten(3)
OUTPUT :
6. Write a python program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payments, insurance, gas, oil, tires, and maintenance. The program then should display the total monthly cost of their expenses and the total annual cost of these expenses.
So, we will first create function1.py file and then define the function Function_one().
def Function_one():
loan_payments=0
insurance=0
gas=0
oil=0
tires=0
maintenance=0
Now we will create another .py file and we will define Function_two() there and then we will use all the attributes of Function_one() .
# importing the function1 from function1.py
from function1 import Function_one
# taking user inputs
def Function_two():
Function_one.loan_payments=float(input("Enter the loan amount :
"))
Function_one.insurance=float(input("Enter the insurance amount :
"))
Function_one.gas=float(input("Enter the gas amount : "))
Function_one.oil=float(input("Enter the oil amount : "))
Function_one.tires=float(input("Enter the tires amount : "))
Function_one.maintenance=float(input("Enter the maintenance amount
: "))
monthly_expense=(Function_one.loan_payments+Function_one.insurance+Function_one.gas+Function_one.oil+Function_one.tires+Function_one.maintenance)
# Printing the monthly expense and annual expense
print("\nThe total monthly_expense :
{}".format(monthly_expense))
print("The total annual_expense :
{}".format(monthly_expense*12))
#under the main function we are calling Function_two
if __name__=="__main__":
Function_two()
OUTPUT: