Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Last active January 19, 2021 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/ac852561203c8bea1237002add0e2614 to your computer and use it in GitHub Desktop.
Save sojohnnysaid/ac852561203c8bea1237002add0e2614 to your computer and use it in GitHub Desktop.
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"{student.name} is in {student.dorm}.")
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