2016-11-26 7 views
-1

私はプログラミングに非常に新しいですし、簡単な選択肢のゲームをしていた。シンプルな選択ゲームPython IDLE 3.4どのように別の選択肢を進めるには?

Answer = (input("You meet a bear, what do you do? A) Give the bear a hug B) Run away")) 
if Answer == ("A)"): 
     print("The bear chopped your hand off!") 
else: 
     print("Good choice, but the bear is running after you") 

しかし、私はどのように上に行くのですか?好きなように手を加えたり、森を走ったりした後にオプションを追加する(少なくとも前の2つの結果については2つの選択肢があります)

+1

ゲームを試してみてください

def main(): print("Tristan Aljamaa's Simple Python Choice Game") print("===========================================") print("Instructions: Type A) or B) etc. whenever prompted\n") game() def game(): Answer = (input("You meet a bear, what do you do?\n A) Give the bear a hug\n B) Run away \nEnter A) or B):")) if Answer == ("A)"): print("The bear chopped your hand off!") player_died() else: print("Good choice, but the bear is running after you") player_ran() def player_died(): print("You died, the bear eventually ate you...") Answer = (input("Game Over!\n\nEnter N) for New Game:")) if Answer == ("N)"): main() else: print("Good Bye!") def player_ran(): Answer = (input("You find an exit from the forest, what do you do\n A) Exit forest\n B) Run around in forest \nEnter A) or B):")) if Answer == ("A)"): print("You exited the forest") player_crossedRoad() else: print("You (although insanly) chose to run around in the forest") player_stillRunsinForest() def player_crossedRoad(): print("You get the idea...") main() #for testing on this online editor use the below line when calling the .py file on your computer if __name__ == "__main__":main() 

:)に展開する方法を見つけ出すことができますスタートですあなたは欲しい。別のレベルのインデントを追加するだけです。 –

+0

ゲームのデシジョンツリーアーキテクチャを探しているようです。これを実装する方法について議論する偉大なリソースがたくさんあります。ウィキペディアは良いスタートになるでしょう。 https://en.wikipedia.org/wiki/Decision_tree – rfj001

答えて

0

はあなたがうまくいけば、することはできますが場合/他の場合/他の内部何度でも出here

+0

"player_crossedRoad"の後の "else"はやや不正な構文ですか?あなたは何をやっているのですか?\ n A)森林を出る\ n B)森林の周りを走ります\ nEnter A)またはB) : ")) 回答==(あれば" A) "): プリント(" あなたは森を出た ") player_crossedRoad()他の : プリント(" あなた(insanlyものの)が森の中で走り回ることにしました") player_stillRunsinForest if __name__ ==" __main __ ":main() –

+0

あなたの質問はありますか? – shash678

+0

完全にランダムな無効な構文です。他の文字と同じように見え、後にコロンが付きます。 –

0

さまざまなケースで異なる機能/手順を作成できます。たとえば:

def choppedHand(): 
    selection = input("The bear chopped your hand off! What do you do now? \n 1.Fight the bear with one hand.\n 2. Scream and search for help.\n 3. Cry and beg for mercy") 
    if selection == "1": 
     fight() 
    elif selection == "2": 
     scream() 
    else: 
     cry() 

def run(): 
    selection = input ("Good choice, but the bear is running after you. What do you do now? 1.Run through the trees. 2.Run in the plain") 
    #etc etc, same as top. 

Answer = (input("You meet a bear, what do you do?\n 1.Give the bear a hug.\n 2.Run away.")) 
if Answer == ("1"): 
    choppedHand() 
else: 
    run() 

これはあくまでも一例ですが、別の例のためのさまざまなオプションを作成して、あなたのコードの他の部分で関数を呼び出すことができる関数を使用します。たとえば、キャラクターは別の状況で腕を失う可能性があります。この場合、関数choppedHand()を呼び出す必要があります。

これはあなたが探していたことを希望しています。ここで

関連する問題