Python word game, functions wont work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Johnson
    New Member
    • Dec 2011
    • 8

    Python word game, functions wont work?

    I have been going through the book: learn python the hard way, and i have come to exercise 43, where you build your own 'game'. This is my attempt so far, all the functions are working, except for the death() function? why wont it work?

    When i type: python game.py>admin>l eft it should say that i die, but it goes onto the "right" option as well?


    here it is:

    Code:
    from sys import exit
    from random import randint
    
    
    
    
    def main_hall():
        print "You are on the war ship. You need to get into the elevator to get to the bridge; where the General is."
        print "The whole destroyer is on alert and nearly every door is locked. There is a keypad on the elevator."
        print "There is a warning saying you only have 10 attemps, then the system deactivates and you cannot complete your mission. Be careful!"
        
        combination = "%d%d%d"  % (randint(1,9), randint(1,9), randint(1,9))
    
        guess = raw_input ("[Keypad:] ")
    
        guesses = 0
    
        while guess != combination and guesses < 10 and guess != "admin":
            print "*BZZT!* ERROR 2355: Incorrect combination!"
            guesses += 1
            guess = raw_input ("[Keypad:] ")
    
        if guess == combination:
            print "You open the elevator and start going up, fast. "
            return 'junction'
    
        elif guess == "admin":
            return 'junction'
    
        else:
            print "You have had more than 10 attempts! \n A small self destruct system in the lock blows you up!\n"
            return 'death'
            
            
            
    
    main_hall()
    
    
    
    def junction():
        print "When you get to the top, you are at a T-junction. Do you go left or right?"
        action = raw_input ("> ")
    
        if action == "left":
            print "You start running left, gun in hand, until you come across a turn, with some dark mist. You attempt to run through the mist, but you start to choke and you cannot get out. You die!"
            return 'death'
    
        elif action == "right":
            print "You start running down the right corridor, gun in hand. You finally make it to the brige where you must take control of the ship. You then reallize there is a crew of around 4-5 people. What do you do?"
            return 'bridge'
    
    def bridge():
        action = raw_input ("Kill;\nPretend you're one of them?\nRun?\n> ")
    
        if action == "kill":
            print "You click the safety off you rifle, and start opening fire. You manage to kill all but one of them. You try to shoot at the last man, but he is too fast! You see him initiate the self destruct! You die!"
            return 'death'
    
        elif action == "pretend":
            print "You walk into the bridge, casually. They dont make any signs of noticing at first, but then they see your uniform and start shouting questions at you. Then, they start shooting you. You start shooting back but it was too late. You die!"
    
        elif action == "run":
            print "You start running back the way you came, until  you find somebody else from you ship. You tell him about what happens, you go back and complete the mission. Congratulations!"
            return 'win'
    
    
    def win():
        print "Horray! You take control of the ship, and become a hero!"
        exit (1)
    
    def death():
        print "Well you really have failed this mission. GO HOME LOSER!"
        exit (1)
    
    junction()
    bridge()
    win()
    death()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Do don't catch any of the returns. Change the last lines to
    Code:
    result = junction()
    if result != "death":
        bridge()
        win()
        death()

    Comment

    Working...