Unit 3 Sections 12 and 13
Calling and Developing Procedures
-
procedure: named group of programming instructions that may have parameters and return values
- can be referred to as method or function
- Parameters: input values of a procedure
- Arguments: specify the values of the parameters when a procedure is called
- Procedure calls
- interrupts the sequential execution of statements causing the program to execute the statements within the procedure before continuing. One the last statement in the procedure (or a return statement) has executed, flow or control is returned to the point immediately following where the procedure was called.
- consider whether the call returns data or a block of statements
-block of statements: call the procedure by referring to the procedure name and inputting the arguments
- Data(boolean or value): assign that value to a variable
- Problem 1
- convert pseudo code to python
- procedure convertFahrenheit converts fahrenheit to celsius
def convertFahrenheit(temperature):
celsius = temperature - 32
celsius = celsius * 0.56
return celsius
outsideTemp = input("what is the temperature outside?")
outsideTemp = (convertFahrenheit(int(outsideTemp)))
print(outsideTemp)
- Developing procedures
- Two Types of Procedures
- one that returns a value
- on that executes a block of statements
- important to pick a descriptive name
- steps: picking a good name, thinking of parameters, making a flowchart, develop procedure
- Two Types of Procedures
- Writing Procedures Activity
quizGrade = 32
def currentGrade(currentPoints):
currentGrade = currentPoints / 40
currentGrade = currentGrade * 100
return currentGrade
newPoints = int(input("how many points do you currently have?"))
newPercent = (currentGrade(int(newPoints)))
if (newPoints > quizGrade):
newquizGrade = newPercent
print("your new grade is: " + str(newquizGrade))
else:
print("your score is still " + str(quizGrade))
- Procedural Abstraction
- one type of abstraction is procedural: provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it
- helpful for managing complexity -Subdivision of a program into separate subprograms is called modularity
- may extract shared features to generalize functionality instead of duplicating code
- allows for program reuse, which helps manage complexity
- naming and calling a pre-written procedure
- include the right arguments so the procedure can do what its supposed to do
- one type of abstraction is procedural: provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it
- AP English Vocab study database
- search/add/delete functions
- add/delete terms.
- quickly search for terms already in the database
- all three functions are called in the main function which is called at the bottom of the code cell
- search/add/delete functions
infoDb = []
infoDb.append({
"Word": "Pristine",
"Definition": "Pure, clean",
})
infoDb.append({
"Word": "Expiation",
"Definition": "Atonement",
})
def print_data2(d_rec): #formatting
print("Word:", d_rec["Word"]) # using comma puts space between values
print("\t", "Definition:", d_rec["Definition"]) # \t is a tab indent
print()
def data_entry(): #defining the function that asks for user input
Word = input("What vocab word would you like to save in the database?")
Definition = input("What is the definition of the vocab word?")
infoDb.append({ #appends the user input to the dictionary
"Word": Word,
"Definition": Definition,
})
def search_data(word):
for record in infoDb:
if record["Word"] == word: #compares the already existing word to the word inputted with the word variable
return record
return NULL
def data_delete(word):
record = search_data(word) #defines record as the word inputted with the search function
if (record != NULL): #if the record doesn't equal null (does it exist?) then the next line removes it
infoDb.remove(record)
print(word, "has been deleted!")
else:
print("Record not found!")
def main():
Continue = True #defining continue as true
while Continue:
lol = input("What would you like to do (add/search/delete, type no if you want to exit)?")
if lol == "no":
print("Come back again!")
Continue = False
elif lol == "add":
data_entry()
elif lol == "search":
word = input("What word do you want to search?")
record = search_data(word) #defines record as the input "word" and runs it through the search function
print_data2(record)
elif lol == "delete":
word = input("What word do you want to delete")
data_delete(word)
else:
print("Invalid input. Please try again")
length = len(infoDb) #defines length as the number of records
print("Total Number of Words: ", length)
for record in infoDb:
print_data2(record)
main()