2017-01-02 36 views
0

我試圖創建是這樣的函數:功能用於轉換比(Python的[2.7])

def ratio_transform(n, h, nh): 
    ... 

換句話說,你的分數爲n/h,並且返回的值爲x/NH

例如,我想轉換百分之五於x/1000(n爲5,h爲100和NH是1000)

我的代碼變爲:

def ratio_transform(n, h, nh): 
    return (n/h) * nh 

但是,ratio_transform(5,100,1000)總是返回0.0,當它返回0.5時

它是邏輯錯誤還是數據類型錯誤?

+2

在Python 2 5/100爲0 – Li357

+0

有點相關:http://stackoverflow.com/q/183853/5647260 – Li357

回答

1
def ratio_transform(n, h, nh): 
    return (n/float(h)) * nh 
+0

當我打電話ratio_transform(5,100,1000),它返回50.0 –