python - abbreviation program with index accessing from a file -


i have this:

print ('ask me abbreviations :)')  v_file = open('abbreviations.txt','r') v_abbrv = v_file.read() v_exit = 'yes'  while v_exit != 'no':     v_word = str(raw_input('abbreviation ? '))     if v_word == 'lol':         print 'lol means:',(v_abbrv[5:19])     elif v_word == 'rolf':         print 'rolf means :',(v_abbrv[26:55])     elif v_word == 'ace':         print 'ace means: ',(v_abbrv[61:78])     elif v_word == 'ad':         print 'ad means: ',(v_abbrv[83:95])     elif v_word == 'afair':         print 'afair means: ',(v_abbrv[103:123])     elif v_word == 'afk':        print 'afk means:',(v_abbrv[129:147])     elif v_word == 'ani':        print 'ani means:',(v_abbrv[153:170])     elif v_word == 'cul':        print 'cul means:',(v_abbrv[175:189])     elif v_word == 'cwyl':        print 'cwyl means:',(v_abbrv[196:216])     elif v_word == 'iq':        print 'iq means:',(v_abbrv[220:238])     elif v_word == 'ba':        print 'ba means:',(v_abbrv[243:259])     elif v_word == 'bs':        print 'bs means:',(v_abbrv[264:284])     elif v_word == 'ma':        print 'ma means:',(v_abbrv[288:302])     elif v_word == 'jd':        print 'jd means:',(v_abbrv[307:319])     elif v_word == 'dc':        print 'dc means:',(v_abbrv[324:346])     elif v_word == 'pa':        print 'pa means:',(v_abbrv[346:369])     elif v_word == 'md':        print 'md means:',(v_abbrv[371:391])     elif v_word == 'vp':        print 'vp means:',(v_abbrv[396:410])     elif v_word == 'svp':        print 'svp means:',(v_abbrv[416:437])     elif v_word == 'evp':        print 'evp means:',(v_abbrv[443:467])     elif v_word == 'cmo':        print 'cmo means:',(v_abbrv[473:497])     elif v_word == 'cfo':        print 'cfo means:',(v_abbrv[502:524])     elif v_word == 'ceo':        print 'ceo means:',(v_abbrv[531:555])     v_exit = str(raw_input('ask more?yes\\no: ')) 

and contents of loaded text file:

lol, laugh out loud rolf, rolling on floor laughing ace, cool experience ad, awesome dude afair, far remember afk, away keyboard ani, age not important cul, see later cwyl, chat later iq, ignorance quotient ba, bachelor of arts bs, bachelor of science ma, master of arts jd, juris doctor dc, doctor of chiropractic pa, personal assistant md, managing director vp, vice president svp, senior vice president evp, executive vice president cmo, chief marketing officer cfo, chief financial officer ceo, chief executive officer 

i finished couple of minutes ago , works fine have feeling not way doing though, isn't it? i'm curious on opinions out there. said if works, it's ok, don't think can in case. think? thanks...

you'd better first read file dictionary:

abbrevs = {} open('abbreviations.txt') f:     line in f:         short, long = line.rstrip().split(',', 1)         abbrevs[short] = long 

you can find abbreviation simple lookup in dictionary:

v_word = str(raw_input('abbreviation ? ')) if v_word not in abbrevs:     print "don't know means." else:     print "{} means {}".format(v_word, abbrevs[v_word]) 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -