2013-05-07 141 views
0

我對Python很陌生,試圖弄清楚如何減去用戶輸入數組中的數字。例如,早在我的計劃,我有: `從輸入數組中減去數字

peakone = raw_input("Where is the next large peak?: ") 
next_peak.append(peakone) 
peaktwo = raw_input("Where is the next large peak?: ") 
next_peak.append(peaktwo) 

現在我想從peaktwo減去peakone並保存此值作爲第三。如果有,最好的辦法是什麼?

+0

是數字:考慮寫這樣的代碼整數? – Ofiris 2013-05-07 16:13:19

回答

0

你可以這樣說:

peakthree = float(peaktwo) - float(peakone) 

你從raw_input得到的是一個字符串,所以你需要將其轉換爲數字類型,像floatint。如果需要的話

def get_next_peak(): 
    return float(raw_input("Where is the next large peak?: ")) 

next_peak = [] 
next_peak.append(get_next_peak()) 
next_peak.append(get_next_peak()) 
next_peak.append(next_peak[1] - next_peak[0]) 
0

設置輸入next_peaklist

演員到intfloat

next_peak = [] 
peakone = raw_input("Where is the next large peak?: ") 
next_peak.append(int(peakone)) 
peaktwo = raw_input("Where is the next large peak?: ") 
next_peak.append(int(peaktwo)) 
print next_peak[0] - next_peak[1] 

可以改變intfloat