Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在某些情况下%r可以工作而%d可以工作,即使有一个数字_Python_Python 2.7 - Fatal编程技术网

Python 为什么在某些情况下%r可以工作而%d可以工作,即使有一个数字

Python 为什么在某些情况下%r可以工作而%d可以工作,即使有一个数字,python,python-2.7,Python,Python 2.7,这里是Python初学者。我想问你一个非常简单的问题 这是第一个示例代码:- 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) print "I w

这里是Python初学者。我想问你一个非常简单的问题

这是第一个示例代码:-

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)
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)
在最后一行中使用%d会产生错误:

   TypeError: %d format: a number is required, not str
这是第二个示例代码:-

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)
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)
这不会给出第一个代码给出的错误


所以我的问题是为什么使用%d会给我错误,而使用%r不会给我错误

每个变量都有一个未声明的隐式类型。类型是数字或字符串(文本)<代码>原始输入总是返回字符串

##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)

%d
标志尝试将变量格式化为数字。当它找到文本时,会抛出一个错误。

当您通过
raw_input()
进行输入时,它会返回一个字符串,因此
x
y
是字符串,
z
x
y
的串联,而不是它的加法。我不确定这是否是你想要的。如果希望将它们作为
int
,请使用
int(原始输入(…)
)将它们转换为int

出现错误是因为%d希望
x
y
z
(用于替换
%d
)为整数(但它们实际上是字符串,因此出现错误)


%r
表示接受任何类型对象的
repr()
的输出,因此它在第二种情况下工作,尽管它将返回串联(而不是加法).

使用原始输入时,必须将字符串转换为所需的数据类型。例如,在我的示例1中,我使用int()函数将变量x和y转换为数据类型int。或者,您可以只使用输入,Python将为您解决这个问题,例如,如果在输入时输入一个数字,Python将假定它是一个整数,如果您键入一个字符串,则它将假定它是一个字符串

##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)
per raw_input接受您的输入并将其指定为字符串

%d只格式化数字

per(很难找到,%r没有很好的文档记录)%r使用convert\u字段将变量转换为表示形式,该表示形式在解析时将获得相同的值


我相信+是将两个字符串(x和y)强制为数字,以便它们可以相加

%d
正在请求一个数值<代码>原始输入提供字符串值。将两个字符串值相加将生成一个字符串值。因此,您在需要数字值的位置传递字符串值。您是否阅读了%转换类型?相关:但基本上,
%r
调用
repr
非常感谢,伙计。我现在明白了。所以,基本上%d=整数,而不是其他任何东西。现在我有另一个简单的问题。当我运行上面的代码并键入这两个数字时,我总是得到错误的答案。代码运行成功,但结果总是错误的。因此,如果我键入第一个数字7和第二个数字3,正确答案应该是,但程序告诉我答案是73。为什么会发生这种情况?你是否阅读了答案中的第一段,这就解释了你为什么会遇到这个问题,以及如何解决它。我使用了你建议的“int(raw_input())”,现在效果非常好。很高兴我能提供帮助,如果答案对你有帮助,我想建议你接受答案(通过点击答案左侧的勾号),这将对社区有帮助。非常感谢你。我现在明白了。我喜欢你的例子1,我从来没有这样想过。