2

It's a very basic doubt in Python in getting user input, does Python takes any input as string and to use it for calculation we have to change it to integer or what? In the following code:

a = raw_input("Enter the first no:")
b = raw_input("Enter the second no:")
c = a + b
d = a - b
p = a * b
print "sum =", c
print "difference = ", d
print "product = ", p 

Python gives following error:

Enter the first no:2
Enter the second no:4
Traceback (most recent call last):
File "C:\Python27\CTE Python Practise\SumDiffProduct.py", line 7, in <module>
d=a-b
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Can someone tell please why am I getting this error?

Santosh Kumar
28.3k21 gold badges72 silver badges126 bronze badges
asked Aug 20, 2013 at 11:54
1
  • User inputs are strings. Use int() before doing the operations. Commented Aug 20, 2013 at 12:03

4 Answers 4

2

Yes, every input is string. But just try:

a = int(a)
b = int(b)

before your code.

But be aware of the fact, that user can pass any string he likes with raw_input. The safe method is try/except block.

try:
 a = int(a)
 b = int(b)
except ValueError:
 raise Exception("Please, insert a number") #or any other handling

So it could be like:

try:
 a = int(a)
 b = int(b)
except ValueError:
 raise Exception("Please, insert a number") #or any other handling
c=a+b
d=a-b
p=a*b
print "sum =", c
print "difference = ", d
print "product = ", p 

From the documentaion:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

answered Aug 20, 2013 at 11:59
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you are correct thinking you need to change the input from string to integer.

Replace a = raw_input("Enter the first no: ") with a = int(raw_input("Enter the first no: ")).

Note that this will raise a ValueError if the input given is not an integer. See this for how to handle exceptions like this (or use isnumeric() for checking if a string is a number).

Also, beware that you although you might find that replacing raw_input with input might work, it is a bad and unsafe method because in Python 2.x it evaluates the input (although in Python 3.x raw_input is replaced with input).

Example code could therefore be:

try:
 a = int(raw_input("Enter the first no: "))
 b = int(raw_input("Enter the second no: "))
except ValueError:
 a = default_value1
 b = default_value2
 print "Invalid input"
c = a+b
d = a-b
p = a*b
print "sum = ", c
print "difference = ", d
print "product = ", p 
answered Aug 20, 2013 at 11:59

Comments

0
a = input("Enter integer 1: ")
b = input("Enter integer 2: ")
c=a+b
d=a-b
p=a*b
print "sum =", c
print "difference = ", d
print "product = ", p 

Just use input() and you will get the right results. raw_input take input as String.

And one more i Would Like to Add.. why to use 3 extra variables ?

Just try:

print "Sum =", a + b
print "Difference = ", a - b
print "Product = ", a * b 

Don't make the Code complex.

answered Aug 20, 2013 at 12:07

Comments

0

raw_input() stores the inputted string from user as a "string format" after stripping the trailing new line character (when you hit enter). You are using mathematical operations on string format that's why getting these errors, first cast your input string into some int variable by using a = int(a) and b = int(b) then apply these operations.

Santosh Kumar
28.3k21 gold badges72 silver badges126 bronze badges
answered Aug 20, 2013 at 12:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.