[Tutor] Help

Dave Angel davea at davea.name
Sat Feb 2 03:07:36 CET 2013


On 02/01/2013 08:47 PM, Jack Little wrote:
> I get this error
>
> Traceback (most recent call last):
>    File "C:\Users\Jack\Desktop\python\g.py", line 56, in <module>
>      path1pt1()
> NameError: name 'path1pt1' is not defined
>
> With this amount of code:
>
>
> def simpstart():
>    global ammo1
>    global ammo2
>    global ammo3
>    global health
>    global tech_parts
>    global exp
>    global radio_parts
>    ammo1=10
>    ammo2=0
>    ammo3=0
>    health=100
>    tech_parts=0
>    exp=0
>    radio_parts=0

You've stopped defining that function, and now are doing top-level code

This following stuff belongs in a function, probably called main()

> print "You awake in a haze. A crate,a door and a radio."
> g1 = raw_input("Which do you choose  ")
> if g1 == "CRATE" or g1 == "Crate" or g1 == "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
> elif g1 =="DOOR" or g1 == "Door" or g1 == "door":
>        print "You are outside"
> elif g1 == "RADIO" or g1 == "Radio" or g1 == "radio":
>        print "It's only a few parts"
>        radio_parts=radio_parts+3
> g2 = raw_input("So this is NYC.Ten years after.There are a few streets.Go west or north  ")
> if g2 == "WEST" or g2 == "West" or g2 == "west":
>        path2_pt1()
> elif g2 == "NORTH" or g2 == "North" or g2 == "north":
>        path1pt1()

Now, in top-level code, you're trying to call a function that hasn't 
been defined yet.  If you want to have complete freedom of order when 
you define your functions, don't put any top-level code except a tiny 
block at the end of the script.  No function may be called before it has 
been defined.  But inside a function, the call won't be made till the 
function is called.  So if the only calls happen at the end of the 
script, there'll never be such a problem.


>
>
> def path1pt1():
>      print "This is where it all started. Freedom Tower. A biotech firm called Aesthos Biotechnology. Based here."
>      print "I worked there."
>

This should be the only non-trivial top-level code, and it should be at 
the end of the script.

if __name__ == "__main__":
     simpstart()
     main()


-- 
DaveA


More information about the Tutor mailing list