0

I'm very new to Python and trying to figure out how to subtract numbers that are in arrays that were user inputted. For example, earlier in my program I have: `

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)

Now I want to subtract peakone from peaktwo and save this value as a third. What is the best way to do this, if there is one?

1
  • Are the numbers integers ?
    – Ofiris
    May 7, 2013 at 16:13

2 Answers 2

0

You can do it like this:

peakthree = float(peaktwo) - float(peakone)

What you get from raw_input is a string, so you need to convert it to a numeric type, like float or int. Consider writing your code like this:

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

Set next_peak as a list

Cast the input toint or float

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] 

Can change int to float if needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.