擅长:python、mysql、java
<p>在<code>print("%d, %s" % number, text)</code>语句中,您需要为字符串格式提供一个参数元组(在您的例子中是“%d,%s”)。所以,正确的方法是-</p>
<pre><code>print("%d, %s" % (number, text))
</code></pre>
<p>或者</p>
<pre><code>print number, text # applicable for python 2.7
# print (number, text) # Python 3. Python 2.7 prints a tuple
</code></pre>
<p>第二个是适用的,因为您不打印任何其他变量。
对于第一个,您需要一个元组,用于在字符串格式化期间提供多个参数的位置。所以,如果想打印只说数字,可以只<code>print ("%d" %number)</code>或<code>print "%d" %number</code>。本例中的元组不是强制的,但是如果您真的想键入更多的字符,则可以执行<code>print ("%d" %(number,))</code>。你知道吗</p>
<p>但第一种是老办法。对于更新和更酷的版本,请使用<code>format()</code>如下-</p>
<pre><code>print("{}, {} blah blah blah".format(number, text))
</code></pre>
<p>或者</p>
<pre><code>print "{}, {} blah blah blah".format(number, text)
</code></pre>
<p>有关详细信息,请查看-<a href="https://docs.python.org/2/library/string.html#format-string-syntax" rel="nofollow noreferrer">https://docs.python.org/2/library/string.html#format-string-syntax</a></p>
<p>对于代码中的其他问题,也要遵循@lenik给出的答案。你知道吗</p>