I quite like this extra credit assignment I did for Zed Shaw's Learn Python The Hard Way. Its a neat little text adventure game that uses stats. I would like to eventually expand this, but let me know how the code looks so far! # Legend Of Doobieusimport random from sys import exit #rolls 3d6def roll_3d6():    a = random.randint(1, 6)    b = random.randint(1, 6)     c = random.randint(1, 6)    list = [a, b, c,]    list.sort()    add = sum(list[0:3])    return add#statsdef display_stats():    global pow, cun, per    pow = roll_3d6()    print Power: , pow    cun = roll_3d6()    print Cunning: , cun    per = roll_3d6()    print Personality: , per# Check Random Encounterdef chk_rn():    x = random.randint(1, 6)    if x == 1:        rn_ec1()# First Encounter (main program really)def fi_en():    global pow, cun, per    printIt smells of damp vegetation, and the air is particularly thick. You can hear some small animals in the distance. This was a nice place to sleep.1. Stay close, find some cover, and wait for food to show up.2. Explore the nearby marsh & find the nearest river, following it downstream.3. Walk towards the large mysterious mountain in the distance.     answer = int(raw_input(prompt))    if answer == 1:        cun_one = roll_3d6()        if cun_one <= cun - 2:            printTime passes as eventually you capture some varmints. You feel slightly more roguish.            cun = cun + 1            chk_rn()            fi_en()        else:             print Time passes and a group of slavers marches into right where you are hiding in the woods. They locate you, capture you, and haul you away for a lifetime of servitude in the main city.Goodbye %s % name    elif answer == 2:         power = roll_3d6()        if power <= pow - 4:            printYou trudge through the marshes until you eventually reach a large river. Downstream from the river is a large temple covered in vines, you walk towards it. You feel more powerful.            pow = pow + 2            chk_rn()            te_en()        else:            print The vegetation here wraps itself around your legs making it impossible to move. You will most likely die here in the vegetation. Goodbye %s. % name    elif answer == 3:        cun_two = roll_3d6()        if cun_two <= cun:            print You make your way towards the mountain and you encounter a really large group of devil dogs guarding the entrance to the mountain.            chk_rn()            dd_en()        else:             printYou have gotten lost and ended up right where you started.            fi_en()# Devil Dog Encounterdef dd_en():    global pow, cun, per    printYou have encountered a massive group of Devildogs.1. Fight off the whole group, like a true hero2. Try to sneak very quietly around them. 3. Attempt to tame them like domestic animals. 4. Accept the Devildog curse and walk through unmolested, sort of.    answer = int(raw_input(prompt))    if answer == 1:        power = roll_3d6()        if power <= pow - 6:            print You are a God among men and have defeated all of the surrounding Devildogs. Beyond the entrance to the mountain is a temple covered in vines. You feel more powerful.            power = power + 3            chk_rn()            te_en()        else:             print You are easily overwhelmed by the devildogs and you are continuously eaten by devildogs for all eternity.Goodbye %s. % name    elif answer == 2:        sneak = roll_3d6()        if sneak <= cun - 4:            print You stealthily evade the Devildogs. Beyond the entrance to the mountain is a temple covered in vines. You feel more roguish.            cun = cun + 2            chk_rn()            te_en()        else:            print The devildogs pick up on your scent. You are easily overwhelmed by the devildogs and you are continuously eaten by devildogs for all eternity. Goodbye %s. % name              elif answer == 3:         charm = roll_3d6()        if charm <= per - 4:            print The Devildogs are thrilled and have accepted you as their leader, they allow you to pass and will sing your praises for all eternity. Beyond the entrance to the mountain is a temple covered in vines. You feel more charming.            per = per + 2            chk_rn()            te_en()        else:            print The Devildogs have no idea what you are trying to do.You are easily overwhelmed by the devildogs and you are continuously eaten by devildogs for all eternity. Goodbye %s. % name    elif answer == 4:        printThe Devildogs circle around you and start howling in unison. You feel your life forces draining away as they howl their awful song. Beyond the entrance to the mountain is a temple covered in vines.        pow = pow - 3        cun = cun - 3        per = per - 3        chk_rn()        te_en()    else:        print You stumble as the Devildogs take notice. You are easily overwhelmed by the devildogs and you are continuously eaten by devildogs for all eternity.Goodbye %s. % name# Temple Encounterdef te_en():    global pow, cun, per    print You are now standing right in front of a large temple with many vines covering its cobblestone exterior. An archway precedes a small and peaceful garden with a main road leading to the temple. Next to the archway is a small gargoyle that is moving and staring at youwith a friendly smile. It is now sundown and the area starts to gleam a reddish glow. In the glow you can see faint white whips in the distance beyond the archway. It smells like fresh produce. In your head you are instantly reminded of a heartwarming song you remembered as a kid. 1. Walk through the archway normally towards the temple.2. Run through the archway trying to get to the door as quickly as possible.3. Have a conversation with the gargoyle staring at you.     answer = int(raw_input(prompt))    if answer == 1:        power = roll_3d6()        if power <= pow - 5:            printAs you walk through you immediately feel what feels like hundreds of hands groping at you pulling you into the ground. You stand your ground and make it to the door. You enter the temple. You feel more powerful.            pow = pow + 2            in_te()        else:            print As you walk through, you are taken off guard as hundreds of hands grope you and pull you straight down to hell for the rest of eternity. Goodbye %s. % name    elif answer == 2:        run = roll_3d6()        if run <= cun - 6:            print You sprint through and notice nothing as you quickly arrive at and enter thetemple. You feel more roguish.            cun = cun + 2            in_te()        else:            print You trip and fall as you are running. What feels like 100 hands grabs you and pulls you straight down to hell for the rest of eternity.Goodbye %s. % name    elif answer == 3:        charm = roll_3d6()        if charm <= per - 3:            print You ask the gargoyle where you are, and he cheerfullytells you that you've arrived at the halls of destiny, and that you shouldwalk down the driveway. The sky returns to its normal color. You walk down the passageway and enter the temple. You feel more charming.            per = per + 1            in_te()        else:            print You ask the gargoyle where you are, right as the expression changes on his face and tells you, 'Hell.' Right as hesays that 100 hands come up from the ground and pull you straight to hell.Goodbye %s. % name    elif answer == sing or sing song or Sing or Sing Song:        print The gargoyle rejoices as you sing and exclaims he was just thinking about that song as he saw you. He says you are allowedto enter the temple of destiny. The sky turns back to its normal color. Youwalk down the passageway and enter the temple. You feel charming.        cun = cun + 2        in_te()    else:         printYou stumble as the gargoyle scowls you and screams an awful noise. Immediately, 100 hands appear from the ground and drag you down to hell as the gargoyle cackles above you. Goodbye %s. % name# Inside The Templedef in_te():    global pow, cun, per    printInside the temple, Cobblestone lines the walls. There is one large room leading to another. It smells of mold, and you can hear both water dripping, and gears shifting. In the middle of a room stands a large minotaur with his head looking down carrying a massive axe. 1. Engage the minotaur in combat.2. Sneak along the edges of the walls to the next room.3. Attempt to speak the the minotaur.    answer = int(raw_input(prompt))    if answer == 1:        fight = roll_3d6()        if fight <= pow - 6:            print You destroy the foul beast without taking injury. It screams as it dissolves into the ground. Several torches light up and signal safe entry into the next room. You feel more powerful.            pow = pow + 2            you_win()        else:            print You find yourself unable to best to minotaur in combat and he decapitates you. Goodbye %s % name    elif answer == 2:         sneak = roll_3d6()        if sneak <= cun - 5:            print You successfully sneak around the minotaur and enter the next room. You feel more roguish.            cun = cun + 2            you_win()        else:             print You step on a pressure plate and a blade emerges from the wall decapitating you.Goodbye %s % name    elif answer == 3:        charm = roll_3d6()        if charm <= per - 5:            print You ask the minotaur what his purpose is, and he exclaims to welcome you to your destiny. And allows you to pass. You feel more charming.            per = per + 2            you_win()          else:            printYou ask the minotaur what his purpose is, and he exclaims to prevent you from stealing the hero's destiny. He swings his axe and decapitates you. Goodbye %s. % name    else:        printYou stumble and trip right in front of the minotaur, who takes this opportunityto decapitate you with his axe.Goodbye %s % name        exit# You Win! def you_win():    print You have entered a room covered in jewels & a large crown sits on a pedestal. Behind the crown is a plaque that reads:                        ~CoNgRaTuLaTiOnS HeRo~                  You have found the crown of destiny                  This crown lays here to be worn                   By the hero of this land                  Who has proved himself capable                  Of surviving much hardship, &                  Having both a sharp mind & Spirit                  Take the crown, and henceforth                   Your rightful place as the ruler                  Of Calgaria.You pick up the crown and place it on your head. You are now the king ofCalgaria. Congratulations %s you beat the legend of doobieus!  % name    printYour final stats were: \n, display_stats()    print \nThanks for playing, goodbye!#random encounter 1def rn_ec1():    global pow, cun, per    print You have randomly encountered a colony of large antsThey are about your size but have pincers the size of your arms. Their clicking noises now envelop you...1. Fight off the ants to the best of your ability.2. Run away as quickly as possible.3. Sing 'The Song of Insect Charm.'    answer = int(raw_input(prompt))    if answer == 1:        fight = roll_3d6()        if fight <= pow - 4:            print With an amazing display of strength, you defeat all of your foes. You feel stronger after your victory.            pow = pow + 2        else:            print You are eaten alive by ants and die.Goodbye %s. % name    elif answer == 2:        trick = roll_3d6()        if trick <= cun - 2:            print You outrun the giant ants and have survived.You feel a little bit more roguish            cun = cun + 1        else:            print You fail to outrun the ants and are eaten alive. Goodbye %s % name    elif answer == 3:        charm = roll_3d6()        if charm <= per - 2:            print The giant ants thank you for your lovely rendition & allow you to pass. You feel charming.            per = per + 1        else:             print The giant ants are horrified by your awful rendition oftheir sacred anthem. They tear you apart mercilessly. Goodbye %s. % name     else:        print You stumble as the clicking gets closer and closer untilyou are painfully eaten alive by ants.Goodbye %s % name######################## MAIN PROGRAM ###################################################prompt = > print \n\t\tWelcome to LeGeNeD oF DoObIeUs...print \n\t\tProgrammed using Python 2.7 by Ray Weissname = raw_input(\nWhat is your name brave rogue? > )print \nOk %s, lets roll up some character stats. % nameprint \nPress enter to roll 3D6 for each stat.raw_input()display_stats()print \nThese are your stats. They can be changed by things in game.print \nThese stats will have an effect on the outcomes that you choose.print \nYou will be presented with many choices, type in a number.print \nOr risk typing something different.print \nPress enter to start the gameraw_input()printYou are %s, you are but a wandering rogue in the vast, mysterious, and deadly land of Calgaria. Your goal is to survive. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You have just woken up in the woods, its really cold, and you are hungry. % name fi_en()
LPTHW extra credit game
python
It's difficult to know what advice to give, since some of it is beyond what you've learnt so far (exercise 31?), but...roll_3d6 - there's no need for the sort or list. For the number of items you have, it could just be:def roll_3d6():    return random.randint(1, 6) + random.randint(1, 6) + random.randint(1, 6)or use a loop:def roll_3d6():    add = 0    for _ in range(3):        add += random.randint(1, 6) # add = add + random.randint(1, 6)    return addAs a point to be aware of - you do not want to use existing function names as variable names - like list. Using them will cause you a lot of trouble.display_stats - avoid the use of global if possible. You'll learn about classes, tuples, lists and dictionaries, all of which can be used and passed as parameters/returned as values. This should, btw, be split into two parts, create_stats and display_stats. At the end, you call display_stats which resets everything and doesn't display your current stats.In this case, since you seem to know about lists:POWER = 0CUNNING = 1PERSONALITY = 2def create_stats():    power = roll_3d6()    cun = roll_3d6()    per = roll_3d6()     stats = [ power, cunning, personality ]    return statsdef display_stats( stats ):    print Power: , stats[POWER]    print Cunning: , stats[CUNNING]    print Personality: , stats[PERSONALITY]called as:stats = create_stats()display_stats( stats )This is then used as a parameter to all your functions.def fi_en( stats ): # etcand individual parts can be used:stats[ POWER ] += 2 # stats[POWER] = stats[POWER] + 2chk_rn - don't abbreviate names, it becomes a lot harder to read (and reading it will become far important than writing it). check_random_encounter is just fine as a name.fi_en - whenever you accept input from the user, always validate it. calling int(raw_input(..)) will cause an exception if the user enters text (or even just presses return). The best way in this instance is to test against '1', '2' etc or even have a function to do so.def ask_question( question, allowed_answers ):    # repeat forever until a valid answer is given    while True:         print question        answer = raw_input( prompt ).lower() # test against lower case strings for simplicity        if answer in allowed_answers:            return answerte_en - answer == sing or sing song or Sing or Sing Song The expression doesn't work that way. It should read (with brackets for emphasis):( answer == sing ) or ( answer == sing song ) or ( answer == Sing ) or ( answer == Sing Song )Also, you have the wrong stat advance. You feel charming, but cunning increases, not personality :)in_te - exit should be exit() - calling the function, not using the value. And it isn't even needed. Try also to avoid using the from x import y form. There are occasions when it's useful, but having to write sys.exit() on one occasion instead of exit() isn't one of them :)you_win - print(Your final stats were: \n, display_stats()). display_stats doesn't return anything, so you'll get None written.print(Your final stats were: \n)display_stats()main - don't have significant code outside of a function, it's customary to put that (the main program bit) in a main function and guard it against being run when imported with, e.g.:if __name__ == __main__:    main_program()It's not significant here, but in the future with larger projects, it will be.With regards to the whole structure of the program, I might suggest the following things which should be more or less straightforward:Define a series of global constants (variables) DEAD, FIRST_ENCOUNTER, DEVILDOG_ENCOUNTER etc.For each of your answer options, separate out the code into a separate function, which returns DEAD or the next encounter as appropriate. This will give you options to simplify the code a bit and make it more readable. You may end up with something like:def first_encounter():    answer = ask_question(...etc..., ['1', '2', '3'] )    status = DEAD    if answer == '1':        status = first_encounter_1()    elif answer == '2':         status = first_encounter_2()    elif answer == '3':        status = first_encounter_3()    if status != DEAD:        if chk_rn() == DEAD:            status = DEAD    if status == FIRST_ENCOUNTER:        first_encounter()    elif status == TEMPLE_ENCOUNTER:        temple_encounter()    elif status == DEVILDOG_ENCOUNTER:        devildog_encounter()After you've learnt about while loops, again rewrite your encounter functions, so that instead of calling the next encounter function directly, return the status and call the next encounter from the loop, e.g.def first_encounter():    answer = ask_question(...etc..., ['1', '2', '3'] )    status = DEAD    if answer == '1':        status = first_encounter_1()    elif answer == '2':         status = first_encounter_2()    elif answer == '3':        status = first_encounter_3()    if status != DEAD:        if chk_rn() == DEAD:            status = DEAD    return status....status = FIRST_ENCOUNTERwhile status != DEAD:    if status == FIRST_ENCOUNTER:        status = first_encounter()    elif status == TEMPLE_ENCOUNTER:        status = temple_encounter()    elif status == DEVILDOG_ENCOUNTER:        status = devildog_encounter()    # etcAfter you've learnt about classes, revisit again and see what new changes you can make!