[Tutor] Here is code, no link for my previous question

Alan Gauld alan.gauld at btinternet.com
Mon Jan 7 09:01:57 CET 2013


On 07/01/13 02:07, Jack Little wrote:
> Here is the code, my error is below the code in itallics

That does not look like a normal Python error report. How are you 
running this? If you are using an IDE it may be giving non standard 
messages. It is usually better to run your code from a command line and 
then send us the full error traceback. It contains a lot of useful 
information.

Although in this case the error is sufficiently explicit that we can
pinpoint the error without it.

> global ammo1
> global ammo2
> global ammo3
> global health
> global tech_parts
> global exp
> global radio_parts

These do absolutely nothing, get rid of them.


> def simpstart():
>    global ammo
>    global health
>    global tech_parts
>    global radio_parts
> print "You awake in a haze. A crate,a door and a radio."
> g1 = raw_input("Which do you choose  ")


Remember that indentation is significant in Python.
By moving the indentation of print out to the margin you have ended your 
simpstart() function. As a result simpstart() does absolutely
nothing.

> if g1 == "CRATE" or g1 == "Crate" or g1 == "crate":

The usual way to do this is use the lower() method of strings:

if g1.lower() == 'crate':

>          print "There is a pack of ammo,some food and an odd microchip"
>          ammo1=ammo1 + 6
 >          health=health + 10
 >          tech_parts=tech_parts + 1

You can use the += shortcut to save typing

ammo1 += 6
health += 10
tech_parts += 1

> elif g2 == "NORTH" or g2 == "North" or g2 == "north":
>
>    def path_1pt1():
>      print "This is where it all started. Freedom Tower. A biotech firm
> called Aesthos Biotechnology. Based here."

You are now trying to define a new function inside the elif block.
That's legal Python and you can do it if you really want to, but its 
very unusual style. Normally functions are defined at outer level and 
called when needed.




>
>    def path_1pt2():
>      print "There is a barbed wire topped fence, about 7 feet high.
> There is a building."
>      print "The weather is picking up, and the wind is getting fast and
> violent."
>      p5 = raw_input("Stay outside and risk it or check the inside of the
> building  ")
>      if p5 == "Stay" or p5 == "STAY" or p5 == "stay":
>          print "The wind and snow picked up. You got frostbite"
>          return path_1pt1()
>      elif p5 == "CHECK" or p5 == "Check" or p5 == "check":
>          print "There is a group of survivors huddled in a corner"
>          print  """Survivor 1: Who are you?
>                    Me: Does that matter?
>                    Survivor 2: You aren't of 'em Protectors are ya?
>                    Me: The what?
>                    Survivor 1: They go around killin' people who don't
> comply with their rules and live in New Quebec"
> Me:Huh"""
>          exp=exp+200
>          health=health+50
>          ammo1=ammo1+29
>      p6 = raw_input("Do you wish to take a quest or continue the story?  ")

p6 is defined inside the path_1pt2 function but not returned.

>    if p6 == "QUEST" or p6 == "Quest" or p6 == "quest":

This line is not part of the function but tests p6. That won;t work.
I suspect its an indentation error.
indentation is very important in Python.

>            quest1()

Since the quest() function is not defined in your code I assume you are 
importing it from some other module?


>    elif p6 == "CONTINUE STORY" or p6 == "Continue story" or p6 ==

>    p9 = raw_input("Go east or west  ")
>    if p9 == "EAST" or p9 == "east" or p9 == "East":
>      def GasStation():
>        if p10.lower== "approach kindly":
>            print """Me: Hey Guys!
>            ammo1=ammo1-6

And this is the end of your GasStation() function because the
next line is not indented...

> else:
>          print"They murdered you!"
>          return GasStation()
>          ammo1=ammo1-6

And the above is probably where your error occurs since return is indeed 
outside of a function.

Incidentally if it were in a function the ammo line would never
get executed since it is after the return, so it is pointless.


> else:
>    return path1_pt2()
>    "A snowstorm swept through, killing you."

Same here, the return is outside a function.
And the following string is meaningless

> Here is the error:
> There's an error in your program:
> ***'return' outside function(g.py,line 179)

As I say, please run the program in an environment that prints proper 
python error messages and send the full error text in future.

You need to go away and read about indentation,
function definitions and namespaces.
They are the biggest problems in your code at present.

Also please use bigger indentation size, a single character makes it 
really hard to read and spot which indent lines up with what.
Also these very long control structures likewise make it hard to
look up and see what aligns with what above. (If with else/elif etc)
That all makes your code much harder to maintain and debug.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list