Week 8 Object-Oriented Programming - CS50's Introduction to Programming with Python
raise
There are different paradigms of programming. As you learn other languages, you will start recognizing patterns like these.
Up until this point, you have worked procedurally step-by-step.
Object-oriented programming (OOP) is a compelling solution to programming-related problems.
To begin, type code student.py
in the terminal window and code as follows:
name = input("Name: ") house = input("House: ") print(f"{name} from {house}")
Notice that this program follows a procedural, step-by-step paradigm: Much like you have seen in prior parts of this course.
Drawing on our work from previous weeks, we can create functions to abstract away parts of this program.
`def main(): name = get_name() house = get_house() print(f"{name} from {house}")
def get_name(): return input("Name: ")
def get_house(): return input("House: ")
if name == "main": main()`
Notice how get_name
and get_house
abstract away some of the needs of our main
function. Further, notice how the final lines of the code above tell the compiler to run the main
function.
We can further simplify our program by storing the student as a tuple
. A tuple
is a sequences of values. Unlike a list
, a tuple
can’t be modified. In spirit, we are returning two values.
`def main(): name, house = get_student() print(f"{name} from {house}")
def get_student(): name = input("Name: ") house = input("House: ") return name, house
if name == "main": main()`
Notice how get_student
returns name, house
.