2013-05-15 44 views
-5

我正在學習Python和這裏是一段代碼:劃分兩個int變量在python

x = raw_input('Enter a numerator:') 
y = raw_input('Enter a denominator:') 
print x/y 

這給了我一個錯誤:

Traceback (most recent call last): 
    line 3, in <module> 
    print x/y 
TypeError: unsupported operand type(s) for /: 'str' and 'str' 
+0

錯誤清楚地說:你不能分兩個字符串。之前轉換爲int! – TheHippo

回答

3

更改爲:

x = float(raw_input('Enter a numerator:')) 
y = float(raw_input('Enter a denominator:')) 

raw_input只返回字符串 - 您需要明確地將結果轉換爲數字 - 在此例中爲float,但您也可以使用intdecimal.Decimal