我剛開始編寫代碼,我試圖編寫一個簡單的程序來添加向量。到目前爲止,我有如何使輸入數值
VectorAx= input("What is the x component of Vector A?")
VectorAy= input("What is the y component of Vector A?")
VectorBx= input("What is the x component of Vector B?")
VectorBy= input("What is the y component of Vector B?")
VectorC= "[%s,%s]" % (VectorAx + VectorBx, VectorAy+VectorBy)
print (VectorC)
當我運行腳本一切正常,但輸入不被視爲數字。 例如,如果VectorAx=1
,VectorAy=6
, VectorBx=3
和VectorBy=2
,VectorC
應該是[4,8]
,但是反而它顯示爲[13,62]
。
哎呀!不要在用戶輸入中使用'eval()'!如果有的話,如果有必要,使用'ast.literal_eval()',但不能用於簡單的數字。 –
正確,但我的鏈接進入了'int'內置函數。 – okoboko
正確,但您正在查看'int'(類型)而不是'int'(函數),它是一個內置函數,根據https://docs.python.org/2/library/functions.html #int。 – okoboko