Making a results table in Python
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | juvenile0 = int(input("enter value")) adult0 = int(input("enter value")) senile0 = int(input("enter value")) birth = int(input("enter value")) #generation 1 formulae juvenile1 = adult0 * birth adult1 = juvenile0 * 0.5 senile1 = adult0 + (senile0 * 0.2) #generation 2 formulae juvenile2 = adult1 * birth adult2 = juvenile1 * 0.5 senile2 = adult1 + (senile1 * 0.2) #generation 3 formulae juvenile3 = adult2 * birth adult3 = juvenile2 * 0.5 senile3 = adult2 + (senile2 * 0.2) #generation 4 formulae juvenile4 = adult3 * birth adult4 = juvenile3 * 0.5 senile4 = adult3 + (senile3 * 0.2) #generation 5 formulae juvenile5 = adult4 * birth adult5 = juvenile4 * 0.5 senile5 = adult4 + (senile4 * 0.2) print("____________________________________________") print("|generations |juveniles| adults | seniles |") print("|_____________|_________|_________|_________|") print("|generation 0 | ""%.2f" % juvenile0," |""%.2f" % adult0," |""%.2f" % senile0," |") print("|_____________|_________|_________|_________|") print("|generation 1 | ""%.2f" % juvenile1," |""%.2f" % adult1," | ""%.2f" % senile1," |") print("|_____________|_________|_________|_________|") print("|generation 2 | ""%.2f" % juvenile2," |""%.2f" % adult2," |""%.2f" % senile2," |") print("|_____________|_________|_________|_________|") print("|generation 3 | ""%.2f" % juvenile3," |""%.2f" % adult3," |""%.2f" % senile3," |") print("|_____________|_________|_________|_________|") print("|generation 4 | ""%.2f" % juvenile4," |""%.2f" % adult4," |""%.2f" % senile4," |") print("|_____________|_________|_________|_________|") print("|generation 5 | ""%.2f" % juvenile5," |""%.2f" % adult5," | ""%.2f" % senile5," |") print("|_____________|_________|_________|_________|") |
我想知道用Python创建表的最有效方法。我现在拥有的代码可以工作,但是当用户输入较大的数字时,表就开始中断。有没有更整洁的方法来用python创建一个表?
您可以使用这个库:https://pypi.python.org/pypi/tableate
1 2 3 4 5 6 7 | from tabulate import tabulate table = [ ['generations', 'juveniles', 'adults', 'seniles'], ['generation 0', '%.2f' % juvenile0, '%.2f' % adult0, '%.2f' % senile0] ] print(tabulate(table, tablefmt='grid')) |
如果你不重新发现轮子并使用已经存在的东西呢?
例如,您可以使用:
- 把你想做的都做成表格
- 熊猫也能很好地打印出数据帧。