从输入的数组中减去数字

2024-03-28 20:08:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: the用户inputrawis数字数组where
2条回答

你可以这样做:

peakthree = float(peaktwo) - float(peakone)

raw_input得到的是一个字符串,因此需要将其转换为数值类型,如float或{}。考虑这样编写代码:

^{pr2}$

next_peak设置为list

将输入转换为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

相关问题 更多 >