1

現在、このサイトhttp://inventwithpython.com/chapter6.htmlからチュートリアル「DragonRealm」を実行しています。

私はそれがすべてわずかに何をしているのか理解していますが、それは私の問題ではありません。最後に、プレーヤーが「いいえ」と言った場合、現在持っているように「ひどい*!」と言うコードを少し追加したいと思います。プログラムの最初に戻ります。私はそれを行うことができましたが、2回目に通過すると、yesと入力するかnoと入力するかどうかにかかわらず、再試行するかどうかの入力が表示され、プログラムが終了します。while、if / else、True、Falseの複数の組み合わせを試しましたが、希望する結果が得られません。どうやってそれを続けていくのか分かりませんか?それはおそらく本当に簡単ですが、私はそれを理解することはできません。

これはプログラムのコードです。

import random
import time
def displayIntro():
    print('You are in a land full of dragons. In front of you,')
    print('you see two caves. In one cave, the dragon is friendly')
    print('and will share his treasure with you. The other dragon')
    print('is greedy and hungry, and will eat you on sight.')
    print()
def chooseCave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave will you go into? (1 or 2)')
        cave = input()
    return cave
def checkCave(chosenCave):
    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He opens his jaws and...')
    print()
    time.sleep(2)
    friendlyCave = random.randint(1, 2)
    if chosenCave == str(friendlyCave):
        print('Gives you his treasure!')
    else:
        print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    displayIntro()
    caveNumber = chooseCave()
    checkCave(caveNumber)
    print('Do you want to play again? (yes or no)')
    playAgain = input()
4

1 に答える 1

1

簡単に追加できます、

if 'n' in playAgain:
    print "too bad"
    playAgain = 'yes'

最後に(whileループ内)

ちなみに、これらの2つの行を組み合わせることができます。

print('Do you want to play again? (yes or no)')
playAgain = input()

単純に:

playAgain = input('Do you want to play again? (yes or no)')

input入力を求めるときに文字列引数を表示するためです。

于 2013-03-08T20:51:34.603 に答える