[Tutor] Hi I need advice

Andrew Wilkins toodles@yifan.net
Wed, 7 Nov 2001 22:30:09 +0800


Hi Edy

> My question is why do i can't to use add = DataBase() ?

You can't as you have your code at the moment, but it is possible to get
around that. I'll get to that in a moment.

> class DataBase:
>    def __init__(self, Plan, Domain, Email, Name):

Because you've specified that the class be instantiated with 4 arguments,
these 4 arguments *must* be passed. That is why DataBase('','','','') works
and not DataBase().
To get around this you can specify default values for the arguments:

class DataBase:
    def __init__(self,Plan='',Domain='',Email='',Name=''):

nb. these values can be whatever you like.

test:
>>> class DataBase:
 def __init__(self,Plan='',Domain='',Email='',Name=''):
  self.Plan=Plan
  self.Domain=Domain
  self.Email=Email
  self.Name=Name

>>> add=DataBase()
>>> add.Plan
''

HTH,
Andrew Wilkins

> Hi everyone.
>
>    I need some help/advice over the following code. My 1st attempt using
> class.
>
> #!/usr/local/bin/python
> import sys
>

>       self.UserPlan = Plan
>       self.UserDomain = Domain
>       self.UserEmail = Email
>       self.UserName = Name
>
> print '1. Adding New User'
> print '2. Search New User'
> print '3. View All User'
> print '4. Exit'
> choice = input("Please choose an option: ")
>
> if choice == 1:
>     print 'Adding New User\n'
>     add = DataBase(' ', ' ', ' ', ' ')
>     add.UserPlan = raw_input("Plan: ")
>     add.UserDomain = raw_input("Domain: ")
>     add.UserEmail = raw_input("Email: ")
>     add.UserName = raw_input("Name: ")
>     print "Domain %s has registered" % add.UserDomain
>     save = raw_input("Do you wanna save? Press Y or N\nChoice: ")
>     if save == "Y":
>        print "Record Saved\n"
>        f=open('record', 'w')
>        f.write("Plan: %s\nDomain: %s\nEmail: %s\nName: %s\n-----\n" %
> (add.UserPlan,add.UserDomain
> ,add.UserEmail,add.UserName))
>     elif save == "N":
>        print "No Data Saved\n"
>     else:
>        print "Wrong input ... Bye Bye\n"
>        sys.exit()
>
> elif choice == 2:
>     print 'Search New User\n'
>
>
> elif choice == 3:
>     print 'View All User\n'
>
> elif choice == 4:
>     print 'Exit program ...'
>     sys.exit()
>
> else:
>     print 'Wrong input'
>     sys.exit()
>

>
> Thanks
>
> Edy Lie.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>