Last active
December 31, 2015 09:59
-
-
Save cigrainger/7970757 to your computer and use it in GitHub Desktop.
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 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? ") | |
if newname == 'quit': | |
return 'Okay! Nevermind.' | |
if newname in students.keys(): | |
addnew = raw_input("%s is already in the list. Are you sure you want to add them again? y/n? " % newname) | |
while addnew != 'y' and addnew!= 'n': | |
addnew = raw_input("This is y/n only. %s is already in the list. Are you sure you want to add them again? y/n? " % newname) | |
if addnew == 'y': | |
newemail = raw_input("What is the student's email address? ") | |
if newemail in students.values(): | |
email = students.keys()[students.values().index(newemail)] | |
addnew = raw_input("%s is already used by another student, %s. Are you sure you want to add it to a new student? y/n? " % (newemail, email)) | |
while addnew != 'y' and addnew!= 'n': | |
addnew = raw_input("This is y/n only. %s is already in the list. Are you sure you want to add them again? y/n? " % newname) | |
if addnew == 'y': | |
students[newname] = newemail | |
if addnew == 'n': | |
return "Okay, let's try again." | |
else: | |
students[newname] = newemail | |
elif addnew == 'n': | |
return "Okay, let's try again." | |
else: | |
newemail = raw_input("What is the student's email address? ") | |
if newemail in students.values(): | |
email = students.keys()[students.values().index(newemail)] | |
addnew = raw_input("%s is already used by another student, %s. Are you sure you want to add it to a new student? y/n? " % (newemail, email)) | |
while addnew != 'y' and addnew!= 'n': | |
raw_input("This is y/n only. %s is already in the list. Are you sure you want to add them again? y/n? " % newname) | |
if addnew == 'y': | |
students[newname] = newemail | |
elif addnew == 'n': | |
return "Okay, let's try again." | |
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 prompt 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