Reputation: 517
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 ?
Upvotes: 1
Views: 96
Reputation: 3236
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.
Upvotes: 0
Reputation: 90919
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) .
Upvotes: 1
Reputation: 30
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)
Upvotes: 0
Reputation: 3704
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.
Upvotes: 0