Created
November 3, 2019 22:52
-
-
Save sojohnnysaid/97899d1640faa464465f5bd7a712dc8a to your computer and use it in GitHub Desktop.
File IO in python
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
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)) | |
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