Last active
December 31, 2015 09:19
-
-
Save cigrainger/7966423 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
f = open('students.csv').read() | |
while f.split('\n')[0] != 'name,email': | |
morecsv = raw_input("This does not look like the correct file. Would you like to see more? y/n?") | |
while morecsv != 'y' and 'n': | |
morecsv = raw_input("The prompt was y/n only. Would you like to see more? y/n?") | |
if morecsv == 'y': | |
print f.split('\n')[:10] | |
newcsv = raw_input("Would you like to overwrite this file to create the student list? y/n?") | |
while newcsv != 'y' and 'n': | |
newcsv = raw_input("y/n only. Would you like to overwrite this file to create the student list?") | |
if newcsv == 'y': | |
newfile = open('students.csv','w') | |
newfile.truncate() | |
newfile.write('name,email') | |
newfile.close() | |
f = open('students.csv').read() | |
elif newcsv == 'n': | |
sys.exit('Okay. Goodbye.') | |
elif morecsv == 'n': | |
sys.exit('Okay. Goodbye.') | |
students = {} | |
if f.split('\n') > 1: | |
rows = f.split('\n')[1:] | |
names = [j.split(',')[0] for j in rows] | |
emails = [j.split(',')[1] for j in rows] | |
for i in range(0,len(names)): | |
students[names[i]] = emails[i] | |
def newstudent(): | |
newname = raw_input("What is the student's name? ") | |
newemail = raw_input("What is the student's email address? ") | |
students[newname] = newemail | |
def addstudent(): | |
newstudent() | |
again = raw_input("Do you want to add another student? y/n? ") | |
while again != 'y' and again != 'n': | |
again = raw_input("The promp was y/n only. Do you want to add another student? y/n? ") | |
if again == 'y': | |
addstudent() | |
elif again == 'n': | |
f = open('students.csv','w') | |
f.truncate() | |
f.write('name,email') | |
for i in range(0,len(students.keys())): | |
f.write('\n'+students.keys()[i]+','+students.values()[i]) | |
f.close() | |
addstudent() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment