簡體   English   中英

Python模塊,將值傳遞給args

[英]Python Modules, passing values to args

我正在學習python,目前正在嘗試將值從輸入傳遞給我編寫的模塊的args,但我不知道如何啟動。

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

這是我正在調用的模塊

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

這是代碼im用於調用並將值傳遞給上述模塊中的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()

我想我需要從用戶那里獲取輸入並將其放在類的變量中。 我試過了,但我猜我做錯了一切?

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

如果要將Employee放在其他文件(模塊)中,只需將其放在此處,並from Employee import Employee (您的主要代碼)運行(第一個是模塊,第二個是類)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM