Vote Up 0 Vote Down

python adding variable problem

Hi guys how can i accept two variable to add in this program
please help...

 class cal:
def inti(self,ina,inb):
def add(self):
return self.a+self.b
newcal= cal()
newcal=int(raw_input('enter 2 number:'))
edited on 5/9/14


Vote Up 0 Vote Down
You can do it as follows :

 >>> newcal=inti(input('first value:'), input('second value:') )
flag | link |
Vote Up 0 Vote Down
suppose you enter "34 45" in the input (quotes excluded)
Then raw_input() will return "34 45" -> Which is not a single number.
So you will have to split the value into two strings "34" and "45". The value around which it should be split is " "
a, b = raw_input().split(" ")
now a and b are strings, on which you can call int and convert to two numbers. i.e
a = int(a)
b = int(b)
Now you can use this to add the numbers. like so

 print newcal.inti(a,b)
flag | link |
Vote Up 0 Vote Down
if you used input instead, and separated the numbers with a comma, you could just do

 newcal = cal(*input('Enter two comma separated numbers
flag | link |

Your Answer

Login before answering

Login with facebook