0

Not sure if it is possible but is there a way to multiply,add,or subtract values in a tuple? Say for example I have:

a=raw_input("enter 2 numbers separated by a /: ") #5/2 entered
b=a.split("/")
c=a[0]+a[1]
print c

The value comes out to 52 which is obviously wrong. Is their a way to specify that a[0] is int 5 instead of just the value 5?

2
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by straylight
1

You have to convert to int() or float().
If you concatenate two string then is just strings not numbers.

>>> '5' + '2'
'52'
a = raw_input("enter 2 numbers separated by a /: ") #5/2 entered
b = a.split("/")
c = int(b[0]) + int(b[1])
print c

You can also do it like this if you have a list with string.

>>> l = ['5', '2']
>>> [float(i) for i in l]
[5.0, 2.0]
>>> l = ['5', '2']
>>> map(int, l)
[5, 2]
>>> sum(float(i) for i in l)
7.0

Edited by snippsat: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.