2017-05-26 87 views
-4

我们有这个任务,我花一些时间就可以了...
测试脚本要我们把它打印出来:打印表,而模块

>>> input([0, 1, 2, 3]) 
    x | sin(x) | cos(x) | tan(x) 
--------------------------------- 
    0.00 | 0.00 | 1.00 | 0.00 
    1.00 | 0.84 | 0.54 | 1.56 
    2.00 | 0.91 | -0.42 | -2.19 
    3.00 | 0.14 | -0.99 | -0.14 

不使用模块(我们可以只使用模块MATH使用此作为提示 https://docs.python.org/3/library/string.html#format-specification-mini-language

请帮 这是我现在有:

import math 
def input(list): 
    rad=[] 
    sinvr=[] 
    cosvr=[] 
    tanvr=[] 
    for el in list: 
     sin=math.sin(el) 
     sinvr.append(sin) 
     cos=math.cos(el) 
     cosvr.append(cos) 
     tan=math.tan(el) 
     tanvr.append(tan) 
    print ("  x | sin(x) | cos(x) | tan(x)\n---------------------------------") 
+0

如果这是你的方法只是使用字符串格式来设置你的表格的值? –

+0

您的代码只打印表头 –

回答

2

在python3可以从字符串中使用format方法:

print("{:^10.2f}|{:^10.2f}|{:^10.2f}|{:^10.2f}|".format(x,sin(x),cos(x),tan(x))) 

注意^指中心,10是“细胞”和.2f的总大小被以打印与2位小数的浮子,根据你的需要改变它。

2

首先打印前2行。然后添加此代码:

for i in YOUR_LIST: 
    print '%6.2f | %6.2f | %6.2f | %6.2f'%(i,math.sin(i),math.cos(i),math.tan(i)) 

希望这有助于!

+0

@LutzL感谢您的提醒。我忘了补充。 –