day04notes
py
keyboard_arrow_up
School
Georgia Institute Of Technology *
*We aren’t endorsed by this school
Course
1301
Subject
Mathematics
Date
Apr 3, 2024
Type
py
Pages
2
Uploaded by DrHornet4115
"""
Day 4 - Functions and parameters
1. Recap
a. Collecting input data
b. Intro to functions
2. Functions
a. Declaring
b. Calling
d. Parameters
e. Returning vs printing
f. Combining functions
"""
# === Recap: Collecting user input
"""
destination = input("Where did you go this weekend? ")
print ("Wow " + destination + " is super cool!")
"""
# === Recap: Declaring and calling functions
"""
def funWeekend() : print("Welcome back to campus!")
print("I hope you have an awesome week!")
funWeekend()
funWeekend()
funWeekend()
"""
# === Functions: Parameters and arguments
# Example: Write a function that takes as input the
# name of a park in Atlanta and PRINTS to the shell
# the message "[park name] is fabulous!"
"""
def atlantaParks(parkName):
print(parkName + " is fabulous!")
pN = "Piedmont Park"
atlantaParks(pN) #Piedmont park becomes a parkName
atlantaParks("Grant Park")
"""
# Example: Write a function that takes as input
# the name of a tree and how many growth rings
# are seen in its trunk. The function should PRINT
# the message: "The [name] tree is [age] years old"
def treeAge (treeName, treeRings): #treeName and treeRings are parameter
age = treeRings // 2
print("The " + treeName + " tree is " + str(age) + " years old" )
'''
treeAge("Dogwood", 20) #Dogwood is argument
'''
# >>> Day 04 Miniquiz - Input parameters
# === Functions: Return
# Example: Write a function that takes the area of
# a park and what percentage is covered in grass and
# RETURNS the total grass area of that park.
def grassArea(area, percentGrass):
percentDecimal = percentGrass / 100
gA = area * percentDecimal
return gA #grassArea becomes gA
#print(percentDecimal) #doesn't work because the values is calculated inside the function
result = grassArea(5, 80) # -> result = 4.0
print (result)
print (grassArea(6,60))
# Example: Calculating the volume of soil necessary
# to fill a cylindrical planter. The total volume
# should be RETURNED.
def cylinderVolume(height, radius):
pi = 3.14
volume = pi * radius ** 2 * height
return volume
vol = cylinderVolume(2, 3)
print (vol)
# === Functions: Scope
# Example: Write a function that takes as input
# the dimensions of a planter, the number of
# planters and the volume of a bag of soil and
# calculates how many bags of soil will be needed
# to fill all of the planters. The number of bags
# should be PRINTED to the shell.
def howManyBags(h, r, n, bagVol):
#calculate the vol of single planter volSinglePlanter = cylinderVolume (h, r)
#total vol by multiplying signle planter by the number
totalVol = volSinglePlanter * n # divide total by the vol of single bag of soil
nBags = totalVol / bagVol
print (nBags)
howManyBags (2, 3, 5, 10)
# >>> Day 04 Miniquiz - Return
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help