Friday, 15 June 2012

class and functions in python -



class and functions in python -

i'm new python , i'm trying classes , objects, have script:

#!/usr/bin/env python class test: def __init__(self, username): self.username = username def name_again(self): in range(0-4): print ("username %s" %self.username) ahmed = test('ahmbor') ahmed.name_again()

i'm expecting script print "username ahmbor" 5 times when run script, have nil please help find what's wrong this

you telling range() loop on 0-4 (subtract 4 zero), -4. because default start @ 0 , count up, empty range:

>>> range(0-4) range(0, -4) >>> len(range(0-4)) 0

use comma instead, , utilize 5 loop 5 times, not 4. endpoint not included:

>>> len(range(0, 4)) 4 >>> len(range(0, 5)) 5

python function class python-3.x

No comments:

Post a Comment