osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Not Defined error in basic code


On 3/14/19 9:05 AM, Jack Dangler wrote:
> Just getting started with tutorials and such, and don't understand this -
> 
> <file: class_weapon.py>
> 
> class weapon:
>   ??? weaponId
>   ??? manufacturerName

The previous two lines attempt to refer to existing names,
but neither name exists.

>   ??? def printWeaponInfo(self):
>   ??????? infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
> self.manufacturerName >   ??????? return infoString
> 
> <file: weaponTrack.py>
> 
> import class_weapon
> 
> MyWeapon=weapon()
> MyWeapon.weaponId = 100
> MyWeapon.manufacturerName = "Glock"
> 
> print(MyWeapon.printWeaponInfo)
> 
> executing 'python3 weaponTrack.py' results in this bailing on the first
> element in the class with "not defined". I've been staring at templates
> of this exact structure for about an hour trying to figure out why this
> isn't running at all. Is it simply because it isn't all in one file?
> Thanks for any guidance. Really appreciate the help.

I'm curious about the tutorial you're following.  Depending
on how faithfully you're following along, it seems to
violate some Python conventions, such as CamelCasing class
names and snake_casing (or lowercasing) other names.

Also, when you encounter an error, please copy and paste
the traceback instead of saying "this bailed" so that we
have something to go on rather that having to study your
example from top to bottom to try to figure out what's
wrong.