jeudi 31 décembre 2015

Shortening the code

Is there any way to shorten this code? It works well, there just has to be a way to make it look better.

#d=69+12*log(2)*(f/440)
#d is midi, f is frequency
import math
f=raw_input("Type the frequency to be converted to midi: ")
d=69+(12*math.log(float(f)/440))/(math.log(2))
d=round(d)

The upper part is the converter, and the lower area is the area that needs to be shortened up...

if d==127:
    print("G")
elif d==126:
    print("F#")
elif d==125:
    print("F")
elif d==124:
    print("E")
elif d==123:
    print("D#")
elif d==122:
    print("D")
elif d==121:
    print("C#")
elif d==120:
    print("C")
elif d==119:
    print("B")
elif d==118:
    print("A#")
elif d==117:
    print("A")
elif d==116:
    print("G#")
elif d==115:
    print("G")
elif d==114:
    print("F#")
elif d==113:
    print("F")
elif d==112:
    print("E")
elif d==111:
    print("D#")
elif d==110:
    print("D")
elif d==109:
    print("C#")
elif d==108:
    print("C")
elif d==107:
    print("B")
elif d==106:
    print("A#")
elif d==105:
    print("A")
elif d==104:
    print("G#")
elif d==103:
    print("G")
elif d==102:
    print("F#")
elif d==101:
    print("F")
elif d==100:
    print("E")
elif d==99:
    print("D#")
elif d==98:
    print("D")
elif d==97:
    print("C#")
elif d==96:
    print("C")
elif d==95:
    print("B")
elif d==94:
    print("A#")
elif d==93:
    print("A")
elif d==92:
    print("G#")
elif d==91:
    print("G")
elif d==90:
    print("F#")
elif d==89:
    print("F")
elif d==88:
    print("E")
elif d==87:
    print("D#")
elif d==86:
    print("D")
elif d==85:
    print("C#")
elif d==84:
    print("C")
elif d==83:
    print("B")
elif d==82:
    print("A#")
elif d==81:
    print("A")
elif d==80:
    print("G#")
elif d==79:
    print("G")
elif d==78:
    print("F#")
elif d==77:
    print("F")
elif d==76:
    print("E")
elif d==75:
    print("D#")
elif d==74:
    print("D")
elif d==73:
    print("C#")
elif d==72:
    print("C")
elif d==71:
    print("B")
elif d==70:
    print("A#")
elif d==69:
    print("A")
elif d==68:
    print("G#")
elif d==67:
    print("G")
elif d==66:
    print("F#")
elif d==65:
    print("F")
elif d==64:
    print("E")
elif d==63:
    print("D#")
elif d==62:
    print("D")
elif d==61:
    print("C#")
elif d==60:
    print("C")
elif d==59:
    print("B")
elif d==58:
    print("A#")
elif d==57:
    print("A")
elif d==56:
    print("G#")
elif d==55:
    print("G")
elif d==54:
    print("F#")
elif d==53:
    print("F")
elif d==52:
    print("E")
elif d==51:
    print("D#")
elif d==50:
    print("D")
elif d==49:
    print("C#")
elif d==48:
    print("C")
elif d==47:
    print("B")
elif d==46:
    print("A#")
elif d==45:
    print("A")
elif d==44:
    print("G#")
elif d==43:
    print("G")
elif d==42:
    print("F#")
elif d==41:
    print("F")
elif d==40:
    print("E")
elif d==39:
    print("D#")
elif d==38:
    print("D")
elif d==37:
    print("C#")
elif d==36:
    print("C")
elif d==35:
    print("B")
elif d==34:
    print("A#")
elif d==33:
    print("A")
elif d==32:
    print("G#")
elif d==31:
    print("G")
elif d==30:
    print("F#")
elif d==29:
    print("F")
elif d==28:
    print("E")
elif d==27:
    print("D#")
elif d==26:
    print("D")
elif d==25:
    print("C#")
elif d==24:
    print("C")
elif d==23:
    print("B")
elif d==22:
    print("A#")
elif d==21:
    print("A")
elif d==20:
    print("G#")
elif d==19:
    print("G")
elif d==18:
    print("F#")
elif d==17:
    print("F")
elif d==16:
    print("E")
elif d==15:
    print("D#")
elif d==14:
    print("D")
elif d==13:
    print("C#")
elif d==12:
    print("C")
elif d==11:
    print("B")
elif d==10:
    print("A#")
elif d==9:
    print("A")
elif d==8:
    print("G#")
elif d==7:
    print("G")
elif d==6:
    print("F#")
elif d==5:
    print("F")
elif d==4:
    print("E")
elif d==3:
    print("D#")
elif d==2:
    print("D")
elif d==1:
    print("C#")
elif d==0:
    print("C")

Aucun commentaire:

Enregistrer un commentaire