2012-11-11 127 views
1

我正在學習python,並且正在嘗試將值從輸入傳遞給模塊的參數,但我不知道如何啓動。Python模塊,將值傳遞給參數

有人可以給我一些建議嗎?

這是模塊IM主叫

#!/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 

這是代碼即時通訊使用調用並將值傳遞到ARGS上述模塊

#!/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() 

即時猜測我在需要從用戶那裏獲取輸入並將其放置在類的變量中。我試過,但即時通訊猜測我做了一切錯誤?

+0

大寫的成員/模塊/變量/函數名是unpythonic。代碼中唯一的大寫字母應該是'Employee'和'Att' – Eric

+0

而'Employee.Age == str(Employee.Age)'將始終爲假 - 一個整數永遠不會等於它的字符串表示。 – Eric

回答

1

Employee.Age = int(raw_input("How old are you? ")) 有沒有用的模塊中設置變量,而不是使用一個局部變量,並設置任何你需要設置的Collection1()函數。請注意,您是而不是設置員工(對象)屬性',但模塊的 - 這可能不是你想要的。此外,按照慣例,函數應該以小寫字母命名。

你的繼承模型有點奇怪。爲什麼員工屬於不同的(子)類?通常,屬性會進入主類構造函數。如果你真的想爲屬性使用單獨的類,那麼在這種情況下你根本不應該使用子類。

編輯 這裏就是我想你的意思做:

#!/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 

如果你想在不同的文件(模塊)的員工,只要把它放在那裏,從你的主代碼運行from Employee import Employee(中第一個是模塊,第二個是類)。

+0

嗨,謝謝你的回覆。這些模塊有點奇怪,因爲我爲實踐構建了一個類,創建了另一個類來實踐繼承等。請問我如何爲模塊設置參數值,可以給我一些代碼示例嗎? – k3eper

+0

謝謝埃裏克會記下來。 – k3eper

+0

任何人都可以幫助我什麼即時通訊嘗試達到?我如何使raw_input數據進入模塊im調用的arg? – k3eper