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 并将这个值保存为第三个。如果有的话,最好的方法是什么?

4

2 回答 2

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] 

可以int根据float需要更改

于 2013-05-07T16:14:53.613 回答
0

你可以这样做:

peakthree = float(peaktwo) - float(peakone)

你得到的raw_input是一个字符串,所以你需要把它转换成数字类型,比如floator int。考虑像这样编写代码:

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])
于 2013-05-07T16:11:07.023 回答