2016-11-26 3 views
-1

Je suis très nouveau à la programmation et faisait un jeu simple choix:Choix simple jeu Python IDLE 3.4 Comment procéder avec différents choix?

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") 

Mais comment puis-je continuer? Comme ajouter une option après avoir passé une main coupée ou courir dans la forêt (2 choix au moins pour les deux résultats précédents)

+1

Vous pouvez, mais un if/else dans un if/else autant de fois que tu veux. Ajoutez simplement un autre niveau d'indentation. –

+0

On dirait que vous recherchez une architecture d'arbre de décision pour votre jeu. Il y a beaucoup de bonnes ressources sur la façon de mettre en œuvre cela. Wikipédia serait un bon début. https://en.wikipedia.org/wiki/Decision_tree – rfj001

Répondre

0

Voici commencer, vous pouvez nous l'espérons comprendre comment développer :)

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() 

essayer le jeu here

+0

maintenant le "else" après "player_crossedRoad" est somewhy syntaxe invalide? C'est ce que j'ai fait: def player_ran(): Answer = (input ("Vous trouvez une sortie de la forêt, que faites-vous \ n A) Exit forest \ n B) Courez dans la forêt \ nEntrez A) ou B) : ")) si réponse == (" A) "): print (" vous avez quitté la forêt ") player_crossedRoad() autre: print (" Vous (bien que insanly) a choisi de courir dans la forêt ") player_stillRunsinForest si __nom_name__ ==" __main __ ": main() –

+0

Quelle est votre question? – shash678

+0

La syntaxe invalide complètement aléatoire qui ressemble aux autres et a un deux-points après. –

0

Vous pouvez créer différentes fonctions/procédures pour différents cas. Par exemple:

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() 

Ceci est juste un exemple, mais en utilisant des fonctions, vous pouvez créer différentes options pour différents cas et appeler une fonction dans d'autres parties de votre code. Par exemple, votre personnage peut perdre son bras dans une situation différente et dans ce cas, vous devez juste vous rappeler que vous utilisez la fonction choppedHand(). J'espère que c'était ce que vous cherchiez.