1

Python Beginner here. Would like to ask you a very simple question.

This is the first sample code :-

print "I will \"Add\" any two number that you type."
x = raw_input("What is the first number?")
y = raw_input("What is the second number?")

z = x + y

print "So, %d plus %d equals to %d" % (x, y, z)

Using %d in the last line gives me the error :

   TypeError: %d format: a number is required, not str

This is the second sample code :-

print "I will \"Add\" any two number that you type."
x = raw_input("What is the first number?")
y = raw_input("What is the second number?")

z = x + y

print "So, %r plus %r equals to %r" % (x, y, z)

This does not give the error that the first code gave.

So my question is why using %d gives me the error but using %r does not give me the error ?

user2950680
  • 457
  • 1
  • 5
  • 8
  • `%d` is asking for a numeric value. `raw_input` gives you string values. Adding two string values together results in a string value. Therefore, you're passing a string value in where a numeric value is required. – bedwyr Aug 12 '15 at 01:40
  • Did you read [the documentation](https://docs.python.org/2/library/stdtypes.html#string-formatting) for the % conversion types? – BrenBarn Aug 12 '15 at 01:40
  • related: http://stackoverflow.com/questions/2354329/whats-the-meaning-of-r-in-python – NightShadeQueen Aug 12 '15 at 01:43
  • But basically, `%r` calls `repr` – NightShadeQueen Aug 12 '15 at 01:43

4 Answers4

1

When you take input through raw_input() , it returns you a string, so x and y are strings, and z is the concatenation of x and y , not its addition. Not sure if that is what you intended. If you want them as int , convert them to int by using int(raw_input(...)) .

The error you get is because %d expects x, y and z (used to replace %d ) to be integers (But they are actually strings, hence the error).

Whereas %r which means the output of repr() which accepts any kind of objects, and hence it works in your second case, though it would be returning the concatenation (not addition) .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Thanks a lot, man. I get it now. So, basically %d =integers and not for anything else. Now I have another simple question. When I run the above code and I type the two numbers, I always get the wrong answer. The code runs successfully but the result is always wrong. So if I type the fist number as 7 and the second number 3, the correct answer should be but the program tells me that the answer is 73. Why does this happen? – user2950680 Aug 12 '15 at 02:02
  • Did you read the first paragraph in the answer, that explains why you are having the issue, and how to fix it – Anand S Kumar Aug 12 '15 at 02:04
  • Thanks a lot. I used "int(raw_input())" like you suggested and it works perfectly now. – user2950680 Aug 12 '15 at 02:09
0

Every variable has an implicit type that is not declared. A type is a number or string(text). raw_input always returns a string.

The %d flag tries to format the variable into a number. When it finds text, it throws an error.

yxre
  • 3,576
  • 21
  • 20
0

When you use raw_input you then have to convert the strings to the data type you want for example on my example 1 I am converting the variables x and y to data type int using the int() function. Or you can just use input and Python will take care of that for you for example if on input you enter a number Python will assume is an integer and if you type a string then it will assume is a string.

##Example 1:
print "I will \"Add\" any two number that you type."
x = raw_input("What is the first number?")
y = raw_input("What is the second number?")

z = int(x) + int(y)

print "So, %d plus %d equals to %d" % (int(x), int(y), z)

##Example 2:
print "I will \"Add\" any two number that you type."
x = input("What is the first number?")
y = input("What is the second number?")

z = x + y

print "So, %d plus %d equals to %d" % (x, y, z)
0

per https://docs.python.org/2/library/functions.html#raw_input raw_input takes your input and assigns it as a string.

%d only formats numbers.

per https://docs.python.org/2/library/string.html#format-specification-mini-language (hard to find, %r is not well documented) %r uses convert_field to convert the variable into a representation that will get the same value if parsed.

I believe the + is coercing the two strings (x and y) into numbers so they can be added.

Vynce
  • 2,947
  • 2
  • 15
  • 12