Last active
January 19, 2021 22:57
-
-
Save sojohnnysaid/ac852561203c8bea1237002add0e2614 to your computer and use it in GitHub Desktop.
Python getting started with classes and objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cs50 import get_string | |
from student import Student | |
# Space for students | |
students = [] | |
# Prompt for students' names and dorms | |
for i in range(3): | |
name = get_string("name: ") | |
dorm = get_string("dorm: ") | |
students.append(Student(name, dorm)) | |
# Print students' names and dorms | |
for student in students: | |
print(f"{student.name} is in {student.dorm}.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student: | |
def __init__(self, name, dorm): | |
self.name = name | |
self.dorm = dorm | |
print(f"created a new student object {self.name} living in {self.dorm}!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment