1

Im learning python and am currently trying to pass values from input to the args for a module I wrote but I have no idea how to start.

Can someone give me some advice?

This is the module im calling

#!/usr/bin/python
class Employee:
 'Practice class'
 empCount = 0
 def __init__(self, salary):
 self.salary = salary
 Employee.empCount += 1
 def displayCount(self):
 print "Total Employees %d" % Employee.empCount
 def displayEmployee(self):
 print "Salary: ", self.salary
class Att(Employee):
 'Defines attributes for Employees'
 def __init__(self, Age, Name, Sex):
 self.Age = Age
 self.Name = Name
 self.Sex = Sex
 def display(self):
 print "Name: ", self.Name + "\nAge: ", self.Age, "\nSex: ", self.Sex

This is the code im using to call and pass the values to the args in the above module

#!/usr/bin/python
import Employee
def Collection1():
 while True:
 Employee.Age = int(raw_input("How old are you? "))
 if Employee.Age == str(Employee.Age):
 print "You entered " + Employee.Age + " Please enter a number"
 elif Employee.Age > 10:
 break
 elif Employee.Age > 100:
 print "Please enter a sensible age"
 else:
 print "Please enter an age greater than 10"
 return str(Employee.Age)
def Collection2():
 Employee.Name = raw_input("What is your name? ")
 return Employee.Name
def Collection3():
 while True:
 Employee.Sex = str(raw_input("Are you a man or a woman? "))
 if Employee.Sex == "man":
 Employee.Sex = "man"
 return Employee.Sex
 break
 elif Employee.Sex == "woman":
 Employee.Sex = "woman"
 return Employee.Sex
 break
 else:
 print "Please enter man or woman "
Attributes = Employee.Employee()
Collection1()
Collection2()
Collection3()
Attributes.displayEmployee()

Im guessing I need to take the input from the user and place it in the variables of the class. I tried that but im guessing im doing everything wrong??

asked Nov 11, 2012 at 20:49
2
  • Capitalized member/module/variable/function names are unpythonic. The only capitalized words in your code should be Employee and Att Commented Nov 11, 2012 at 20:54
  • And Employee.Age == str(Employee.Age) will always be false - an integer never equals its string representation. Commented Nov 11, 2012 at 20:57

1 Answer 1

1

Employee.Age = int(raw_input("How old are you? ")) There's no use to setting a variable in the module instead of using a local variable, and setting whatever you need to set outside the Collection1() function. Note that you are not setting the employee (object) atributes', but the module's - this is probably not what you want. Also, functions, by convention, should be named with initial lowercase.

Your inheritance model is a bit strange. Why are the employee attributes in a different (sub) class? Generally, the attributes go into the main class constructor. If you really want to use a separate class for the attributes, you shouldn't use a subclass at all in this case.

EDIT Here's what I think you meant to do:

#!/usr/bin/python
class Employee:
 def __init__(self, salary, age, name, sex):
 self.salary = salary
 self.age= age
 self.name= name
 self.sex= sex
 #Employee.empCount += 1 #don't do this. you should count instances OUTSIDE
 def __str__(self):
 return "Employee<Name: {0}, Age: {1}, Sex: {2}, Salary: {3}>".format( self.name, self.age, self.sex, self.salary)
def getAge():
 while True:
 try:
 s=raw_input("How old are you? ")
 age = int(s)
 if age > 100:
 print "Please enter a sensible age"
 elif age<=10:
 print "Please enter an age greater than 10"
 else:
 return age
 except ValueError:
 print "You entered " + s + " Please enter a number"
def getName():
 return raw_input("What is your name? ")
def getSex():
 while True:
 sex = str(raw_input("Are you a man or a woman? "))
 if not sex in ("man", "woman"):
 print "Please enter man or woman "
 else:
 return sex
age= getAge()
name= getName()
sex= getSex()
salary=100000
employee = Employee(salary, age, name, sex)
print employee

if you want the Employee in a different file (module), just put it there and from your main code run from Employee import Employee (the first is the module, the second is the class).

answered Nov 11, 2012 at 21:03
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, thanks for the response. The modules are abit strange as I built one class for practice the created another to practice inheritance etc. Could you give me an example bit of code on how I set the args values for the module?
Can anyone help me out with what im trying to acheive? How do i make the raw_input data into an arg for the module im calling?
@k3eper check answer edit. Also, for future questions like this, you should use codereview.stackexchange.com
thanks @goncalopp can you please explain this to me def __str__(self): ?
I also notice the "print employee" line refers to the def __str__(self):, how does it know to print that line if you didnt refer to it by its name __str__(self): I know these must be stupid questions but I really do want to understand and learn python.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.