我应该看看这个模型解决方案,以便为我的下一个班级分配工作。该程序返回 32 到 126 之间的 ASCII 值。在“for 语句”之前,我一直在理解它。有人可以帮我看看吗?我知道这与创建四列有关,但我认为在继续之前了解它的每一点都是有益的。
非常感谢。
START = 32
END = 126
def GiveAscii(start=START, end=END, width=4):
"""Returns an ascii chart as a string. Readable."""
entries = end - start +1
entries_per_column = entries/width
if entries % width:
entries_per_column += 1
ret = []
for row in range(entries_per_column):
for column in range(width):
entry = entries_per_column * column + row + start
if entry > end:
break
ret += ["%3d = %-6s" % (entry, chr(entry))]
ret += ['\n']
return ''.join(ret)
def main():
print GiveAscii()
if __name__ == '__main__':
main()