2017-10-09 51 views
2

我對編程非常陌生,並且遇到了一些我無法想象的東西! 我有一個用戶定義的函數,它應該計算百分比:用戶定義的函數在Pycharm中返回不正確的值,但不是IDLE

def test_score(num_correct, total): 
    temp_value = num_correct/total 
    return temp_value*100 

a = 12 
b = 20 
print(test_score(a, b)) 

通過我的計算程序應當返回值60,和它這樣做時,進入IDLE代碼。但是,當輸入到Pycharm中時,代碼返回值0.

有關爲什麼會出現這種情況的任何想法?

+2

他們可能會使用不同版本的Python – DavidG

回答

2

這可能與註釋中指出的Python版本不同。

Python2:

12/20 -> 0 (default for integers) 
12 * 1.0/20 -> 0.6 (if float is involved) 

Python3:

12/20 -> 0.6 (default for integers) 
12 // 20 -> 0 (floor division) 

查看divisionfloor division的文檔。 在Pycharm中,您可以設置settings -> project -> project interpreter(或類似,取決於版本和plattform)的Python版本。

+0

非常好,當num_correct和總數轉換爲函數內的浮點數時,會收到正確的答案。 –