有没有办法缩短这段代码?它运作良好,但必须有一种方法,使它看起来更好.
#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)
上部是转换器,下部区域是需要缩短的区域......
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")
TigerhawkT3.. 6
既然你检查到127 0的整数时,list
或tuple
将是完美的,你可以简单地用索引访问:
>>> l = ['C', 'C#', 'D', 'C', 'C#', 'D'] >>> d = 1 >>> print(l[d]) C#
最重要的是,由于模式重复,您可以使用%
运算符来划分并获取余数,这将允许您使用list
或的单个循环tuple
:
>>> l = ['C', 'C#', 'D'] >>> d = 5 >>> print(l[d%3]) D
使用d%12
而不是d%3
你的情况,因为这是你有多少项目.
由于您使用的是Python 2,通过使用raw_input()
和行为判断round()
,在将其用作索引之前,您需要转换d
为整数d = int(round(d))
.
请注意这些print
语句,因为添加括号不会将其变成Python 3的print()
功能.如果你做的事情就像print('hello', 'world')
你得到的('hello', 'world')
那样,而不是'hello world'
你在Python 3中看到的(或者在print()
导入函数的Python 2中).
既然你检查到127 0的整数时,list
或tuple
将是完美的,你可以简单地用索引访问:
>>> l = ['C', 'C#', 'D', 'C', 'C#', 'D'] >>> d = 1 >>> print(l[d]) C#
最重要的是,由于模式重复,您可以使用%
运算符来划分并获取余数,这将允许您使用list
或的单个循环tuple
:
>>> l = ['C', 'C#', 'D'] >>> d = 5 >>> print(l[d%3]) D
使用d%12
而不是d%3
你的情况,因为这是你有多少项目.
由于您使用的是Python 2,通过使用raw_input()
和行为判断round()
,在将其用作索引之前,您需要转换d
为整数d = int(round(d))
.
请注意这些print
语句,因为添加括号不会将其变成Python 3的print()
功能.如果你做的事情就像print('hello', 'world')
你得到的('hello', 'world')
那样,而不是'hello world'
你在Python 3中看到的(或者在print()
导入函数的Python 2中).