2017-05-26 46 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感謝您的提醒。我忘了補充。 –