Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Created November 3, 2019 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sojohnnysaid/97899d1640faa464465f5bd7a712dc8a to your computer and use it in GitHub Desktop.
Save sojohnnysaid/97899d1640faa464465f5bd7a712dc8a to your computer and use it in GitHub Desktop.
File IO in python
import csv
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))
with open("students.csv", "w") as file:
writer = csv.writer(file)
writer.writerow(("Name", "Dorm"))
for student in students:
writer.writerow((student.name, 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