1

基本的に、var2 でボトルをその数だけ落とすようにしたいです。たとえば、10 を入力してから 2 を入力すると、ボトルは 10 8 6 4 2 0 のように 2 秒でカウントダウンされます。

var = raw_input(" Enter bottle number ")
var2 = raw_input(" Enter how many bottle taken down ")

bottle_number = int(var)
bottle_down = int(var2)

countdown = ""                             

while bottle_number > 0:                      

    print bottle_number, "bottles of beer on the wall,", bottle_number, "bottles of beer."
    print "Take one down and pass it around,", bottle_number - 1, "bottles of beer on the wall."
    print " "
    countdown = bottle_number - 1
    bottle_number = countdown


if bottle_number == 0:                        

    print """
    No more bottles of beer on the wall, no more bottles of beer.
    """
4

1 に答える 1

2

bottle_downの代わりに減算するだけです1。それでおしまい。

もちろん、おそらくテキストも変更したいので、"Take", bottle_down, "down"代わりに"Take one down"、または出力が少し混乱します。

その間…</p>

これを行う理由はありません:

countdown = bottle_number - bottle_down
bottle_number = countdown

ただ行う:

bottle_number = bottle_number - bottle_down

または:

bottle_number -= bottle_down

そして、一番上で初期化する理由はありませんcountdown = ""。その値を使用することはないのに、なぜそれを保存するのでしょうか?

一方、bottle_number - bottle_down2 回計算する (printステートメントで 1 回、次に次のループのために保存する) のではなく、printステートメントの前に 1 回だけ計算してみませんか?

最後に、次のようになります。

if bottle_number == 0:

したがって、 and を付けて実行する92、最終的には 0 ではなく -1 のボトルになるため、この最後の節は出力されません。それはあなたが望むものですか?(また、1 に下がって 2 を取ると、"Take 2 down and pass it around, -1本のビールを壁にかける" という詩が表示されます。化学的に強化されていない数学の新入生—実際に歌うかもしれません.)

于 2013-03-22T20:41:27.440 に答える