Created
March 13, 2014 17:37
-
-
Save cigrainger/9533098 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 | |
| import nltk | |
| 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 fuzzynames(x): | |
| fn = [] | |
| for key in students.iterkeys(): | |
| fn.append(nltk.metrics.edit_distance(x, key)) | |
| if any(i > 5 for i in fn): | |
| while True: | |
| cont = raw_input("%s looks pretty close to %s. Are you sure this is a new student? " % (x,i)) | |
| if cont.strip() == 'y' or 'n': | |
| break | |
| if cont == 'y': | |
| return x | |
| elif cont == 'n': | |
| return 'quit' | |
| ''' | |
| def newstudent(): | |
| newname = raw_input("What is the student's name? ") | |
| # newname = fuzzynames(newname) | |
| 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