2017-09-28 75 views
-2

好吧,所以我開始在我的類中學習python,但我非常惱火如何不能像其他語言一樣在python中聲明變量類型。將這些字符串轉換爲整數的最佳方法是什麼?Python中,如何從字符串中使這些變量整數

for lineNum, line in enumerate(sys.stdin): 
if(lineNum == 0): 
    xc, yc = eval(line) 
    xc = int(xc) 
    yc = int(yc) 
elif(lineNum == 1): 
    r = line 
    r = int(r) 
elif(lineNum == 2): 
    x1, y1 = eval(line) 
    x1 = int(x1) 
    y1 = int(y1) 
else: 
    x2, y2 = eval(line) 
    x2 = int(x2) 
    y2 = int(y2) 
+0

use'int(string_value)'' – kogito

回答

1

你不能在Python中聲明類型,因爲Python是一種動態類型的編程語言,比如Ruby,Smalltalk,Javascript等等。動態語言有自己的優點和缺點,試圖學習它們。

int_number = int('99') # here you go as far as casting is concerned 
相關問題