Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python getting started with classes and objects
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"{} is in {student.dorm}.")
class Student:
def __init__(self, name, dorm): = name
self.dorm = dorm
print(f"created a new student object {} living in {self.dorm}!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment