Started Rectangle Project
This commit is contained in:
commit
26e1253c0d
39 changed files with 544 additions and 0 deletions
45
homework06/functions/functions01.py
Normal file
45
homework06/functions/functions01.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Simple function examples
|
||||
|
||||
#No input parameters
|
||||
def myFunction1():
|
||||
print("You are in myFunction1")
|
||||
|
||||
|
||||
# Two input patrameters
|
||||
def myFunction2(x,y):
|
||||
print("You are in myFunction2")
|
||||
print("x = ",x)
|
||||
print("y = ",y)
|
||||
return(x + y)
|
||||
|
||||
# Two input parameters with predefined valiues.
|
||||
# Provide one or no parameters. If not specified the default values will be used.
|
||||
def myFunction3(x = 2,y = 3):
|
||||
print("You are in myFunction3")
|
||||
print("x = ",x)
|
||||
print("y = ",y)
|
||||
return(x + y)
|
||||
|
||||
# ======================== Main Program =============================
|
||||
|
||||
myFunction1()
|
||||
|
||||
retValue = myFunction2(3,4)
|
||||
print(retValue)
|
||||
|
||||
retValue = myFunction2(5,6)
|
||||
print(retValue)
|
||||
|
||||
# This generates an error
|
||||
#retValue = myFunction2(4)
|
||||
#print(retValue)
|
||||
|
||||
retValue = myFunction3(3,4)
|
||||
print(retValue)
|
||||
|
||||
retValue = myFunction3(5)
|
||||
print(retValue)
|
||||
|
||||
# This does not generate an error
|
||||
retValue = myFunction3()
|
||||
print(retValue)
|
||||
16
homework06/functions/functions02.py
Normal file
16
homework06/functions/functions02.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# list as an input parameter
|
||||
def myFunction4(listParameter):
|
||||
for listItem in listParameter:
|
||||
print("\tlistParameter item: ",listItem)
|
||||
myList = listParameter
|
||||
myList.sort()
|
||||
return( myList ) # Return a list
|
||||
|
||||
# ======================== Main Program =============================
|
||||
|
||||
fruits = ['Mango','Banana','Cherry', 'Apples']
|
||||
print("\n Starting list: ",fruits)
|
||||
print("\n")
|
||||
|
||||
sortedFruits = myFunction4(fruits)
|
||||
print("\n Returned list: ",sortedFruits)
|
||||
25
homework06/functions/functions03.py
Normal file
25
homework06/functions/functions03.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
def updatePerson(personDict):
|
||||
myPerson = personDict
|
||||
|
||||
emailAddress = input("Enter your email address: ")
|
||||
phoneNumber = input("Enter your phone number: ")
|
||||
myPerson['email'] = emailAddress
|
||||
myPerson['phone'] = phoneNumber
|
||||
|
||||
return myPerson
|
||||
|
||||
import pprint
|
||||
|
||||
person = {'Name': 'Zara', 'Age': 7, 'Class': 'First','Color':'Green'}
|
||||
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
pp.pprint(person)
|
||||
print("\n")
|
||||
|
||||
for key in person.keys():
|
||||
print(f" {key:12} {person[key]} ")
|
||||
|
||||
person2 = updatePerson(person)
|
||||
for key in person2.keys():
|
||||
print(f" {key:12} {person2[key]} ")
|
||||
|
||||
40
homework06/functions/functions04.py
Normal file
40
homework06/functions/functions04.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# Simple function examples
|
||||
|
||||
def rectanglePerimeter(rLength,rWidth):
|
||||
'''
|
||||
rLength: length of the rectangle.
|
||||
|
||||
rWidth: width of the rectangle.
|
||||
|
||||
Returns the perimeter of the rectangle.'''
|
||||
perimeter = 2 * (rLength + rWidth)
|
||||
return(perimeter)
|
||||
|
||||
def rectangleArea(rLength,rWidth):
|
||||
area = rLength * rWidth
|
||||
return(area)
|
||||
|
||||
def squarePerimeter(rLength):
|
||||
perimeter = rectanglePerimeter(rLength,rLength)
|
||||
return(perimeter)
|
||||
|
||||
def squareArea(rLength):
|
||||
area = rectangleArea(rLength,rLength)
|
||||
return(area)
|
||||
|
||||
# ======================== Main Program =============================
|
||||
|
||||
length = 2.
|
||||
width = 3.
|
||||
|
||||
retValue = rectanglePerimeter(length,width)
|
||||
print(retValue)
|
||||
|
||||
retValue = rectangleArea(length,width)
|
||||
print(retValue)
|
||||
|
||||
retValue = squarePerimeter(length)
|
||||
print(retValue)
|
||||
|
||||
retValue = squareArea(length)
|
||||
print(retValue)
|
||||
Loading…
Add table
Add a link
Reference in a new issue