0

我已经阅读了许多 python 空格删除问题和答案,但无法找到我正在寻找的内容。这是一个小程序,显示了该问题的具体示例。我非常感谢您的帮助。

import random

math_score = random.randint(200,800)
math_guess = int(input("\n\nWhat score do you think you earned on the math section (200 to 800)?\t"))
print ("\n\n\nOn the math section, you guessed",math_guess,", and your actual score was",math_score,"!")

所以这是我的问题:

当我执行程序时,我得到以下结果:

On the math section, you guessed 600 , and your actual score was 717 !

我想删除句子中每个变量后面的空格。在这种情况下,600 和“,”之间的空格以及 717 和“!”之间的空格。

有没有解决这个问题的标准方法?

4

4 回答 4

4

是的,格式化你的字符串:

print("... you guessed {}, and ... was {}!".format(math_guess, math_score))
于 2012-10-12T16:38:29.490 回答
0

您需要将整行格式化为单个字符串,然后打印该字符串。

print ("\n\n\nOn the math section, you guessed {0}, and your actual score was {1}!".format(math_guess, math_score))
于 2012-10-12T16:37:35.123 回答
0
print ("\n\n\nOn the math section, you guessed",math_guess,", and your actual score was",math_score,"!", sep ='')

如果这是 py3+ 我想

print ("\n\n\nOn the math section, you guessed"+str(math_guess)+", and your actual score was"+str(math_score)+"!")

如果没有应该工作

或按照其他人的建议使用字符串格式...

于 2012-10-12T16:39:45.567 回答
0

试试这个:

print "\n\n\nOn the math section, you guessed %d and your actual score was %d!" % (math_guess, math_score)

您可以在内置类型中阅读更多内容

于 2012-10-12T16:42:45.280 回答