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",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 {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 %d and your actual score was %d!" % (math_guess, math_score)

組み込み型で詳細を読むことができます

于 2012-10-12T16:42:45.280 に答える