Python使用类
我正在使用Python3中的类,并且我在使用它们时遇到了困难。我这里有两个程序(一个正在导入另一个) 这个想法是,你正在制作一份员工名单,你有三个类别的员工小时工资和志愿者 我似乎对每堂课的表演费都有意见。我还知道,在我的工资类中,我试图用一个整数除以一个字符串,但我编写代码的方式我不太确定如何绕过它。 我每小时上课的时间似乎不在名单上 先谢谢你。我真的很困惑,我正在努力完成这个项目 第一个项目(员工) 这是主要节目Python使用类,python,Python,我正在使用Python3中的类,并且我在使用它们时遇到了困难。我这里有两个程序(一个正在导入另一个) 这个想法是,你正在制作一份员工名单,你有三个类别的员工小时工资和志愿者 我似乎对每堂课的表演费都有意见。我还知道,在我的工资类中,我试图用一个整数除以一个字符串,但我编写代码的方式我不太确定如何绕过它。 我每小时上课的时间似乎不在名单上 先谢谢你。我真的很困惑,我正在努力完成这个项目 第一个项目(员工) 这是主要节目 import employee def main():
import employee
def main():
#create list
employees = make_list()
#display list
print('Here are the employees.')
print('-----------------------')
display_list(employees)
def make_list():
#create list
employee_list = []
#get number of hourly employees
number_of_hourly = int(input('\nHow many hourly will be entered? '))
for hourly in range(number_of_hourly):
#get input
name, ID, city = get_input()
base_pay = input('Enter employee base pay: ')
shift = input('Enter employee shift 1,2, or 3: ')
#create object
Hourly = employee.Hourly(name, ID, city, base_pay, shift)
#add object to list
employee_list.append(Hourly)
#get number of salary employees
number_of_salary = int(input('\nHow many salary will be entered? '))
for salary in range(number_of_salary):
#get input
name, ID, city = get_input()
ann_salary = input('Enter employee annual salary: ')
#create object
salary = employee.Salary(name, ID, city, ann_salary)
#add object to list
employee_list.append(salary)
#get volunteers
number_of_volunteers = int(input('\nHow many other volunteers will be entered? '))
for volunteers in range(number_of_volunteers):
#get info
name, ID, city = get_input()
#create object
volunteer = employee.Person(name, ID, city)
#add object to list
employee_list.append(volunteer)
#invalid object
employee_list.append('\nThis is invalid')
#return employee_list
return employee_list
def get_input():
#input name
name = input("Employee's name: ")
#validate
while name == '':
print('\n Name is required. Try again.')
name = input("Employee's name: ")
ID_valid = False
ID = input("Employee's ID: ")
while ID_valid == False:
try:
ID = float(ID)
if ID > 0:
ID_valid = True
else:
print("\nID must be > 0. Try again.")
ID = input("Employee's age: ")
except ValueError:
print("\nID must be numeric. Try again.")
ID = input("Employee's ID: ")
#get city
city = input("Enter employee's city of residence: ")
#return values
return name, ID, city
def display_list(human_list):
#create for loop for isinstance
for human in human_list:
#create isinstance
if isinstance(human, employee.Person):
print(human)
human.show_person()
human.show_pay()
print
else:
print('Invalid employee object')
#call main function
main()
您正在以字符串形式输入工资。转换它
ann_salary = int(input('Enter employee annual salary: '))
你的问题到底是什么?此外,您不需要在所有类变量前面加上
\uu
。它使可读性成为可能。我想,对于课程,show_工资应该改变为该特定课程下的任何工资(小时工资、薪水或志愿者),但出于某种原因,它不起作用。对不起,这件事。我不熟悉这一点,这也是我的书中所说的,所以我不确定是否还有其他方法。事实上,如果上面的编码不起作用,前缀名与之有关。您可能在某个地方看到“这就是在Python中处理私有属性的方式”——严格来说,这是不正确的。但它很容易使这样的代码发生故障。“把它们扔掉吧!”杰里米·伯顿:很遗憾,我还是不明白你在问什么。我看了一遍代码,它看起来很好。@JeremyBorton:你想说你在看哪本书吗?我不知道这种程序在教人们编程方面是否有用。变量名旁边的提示是误导性的,这表明作者对Python相当陌生。
ann_salary = int(input('Enter employee annual salary: '))