如何在python3x中调用if语句中的类和函数

2024-10-05 01:51:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的菜单,可以从If语句调用类和函数。每次我运行代码时,它都会打印“A”。我是python OOP新手。我也试过这里的一些代码。我的代码有什么问题

class CustomError(Exception):
pass

class employee:
    def menu(self):


        print('\tA = To Add Employee Record')
        print('\tB = To Update Employee Record')
        print('\tC = To Increase Employee Salary')
        print('\tD = To Generate Payslip')
        print('\tX = Exit')
        choice = input("\nType here! ").upper()
    def exec_menu(self):
        choice ="A"
        while True:
            if choice == "A":
                rec = add_record  #this is from another class
                rec.employee_info()
            elif choice =="B":
                pass
            elif choice == "C":
                pass
            elif choice == "D":
                pass
            elif choice =="X":
                exit()
            else:
                print("Whong value the system will now exit!")
                exit()


class add_record:

    def employee_info(self):
        print("Employee number must be 9 numbers long")
        print("Name and Lastname must be charcters only")
        print("Plese choose on this Department name: Accounting, Human Resources,  Marketing,  Finance,  MIS,  Admin")
        while True:
            print("*" * 70)
            Employee_empno = int(input("Enter Unique number for your Employee Number:"))
            Employee_last = str(input("Enter Lastname:"))
            Employee_first = str(input("Enter Firstname:"))
            Employee_dept = input("Enter Department:").upper()
            Employee_rate = int(input("Enter Rate per hour:"))

            with open('empRecord.txt', 'a') as f:
                f.write(f"\n{Employee_empno}, {Employee_last}, {Employee_first}, {Employee_dept}, {Employee_rate};")
            print("if you want add more Employee in Information press 1 ")
            user_input = input("Enter:")
        if user_input != "1":


records = add_record()
employee.menu(any)

提前谢谢


Tags: to代码selfaddinputdefemployeepass
1条回答
网友
1楼 · 发布于 2024-10-05 01:51:47

我做了一些修正。尝试:

class CustomError(Exception):
    pass

class Employee:

    def menu(self):
        print('\tA = To Add Employee Record')
        print('\tB = To Update Employee Record')
        print('\tC = To Increase Employee Salary')
        print('\tD = To Generate Payslip')
        print('\tX = Exit')
        return input("\nType here! ").upper()

    def exec_menu(self):
        choice = self.menu()
        while True:
            if choice == "A":
                rec = add_record()  #this is from another class
                rec.employee_info()
            elif choice =="B":
                pass
            elif choice == "C":
                pass
            elif choice == "D":
                pass
            elif choice == "X":
                exit()
            else:
                print("Wrong value the system will now exit!")
                exit()


class add_record:
    def employee_info(self):
        print("Employee number must be 9 numbers long")
        print("Name and Lastname must be charcters only")
        print("Plese choose on this Department name: Accounting, Human Resources,  Marketing,  Finance,  MIS,  Admin")
        while True:
            print("*" * 70)
            Employee_empno = int(input("Enter Unique number for your Employee Number:"))
            Employee_last = str(input("Enter Lastname:"))
            Employee_first = str(input("Enter Firstname:"))
            Employee_dept = input("Enter Department:").upper()
            Employee_rate = int(input("Enter Rate per hour:"))

            with open('empRecord.txt', 'a') as f:
                f.write(f"\n{Employee_empno}, {Employee_last}, {Employee_first}, {Employee_dept}, {Employee_rate};")
            print("if you want add more Employee in Information press 1 ")
            user_input = input("Enter:")

Employee().exec_menu()

只要完成你想用user_input做的事情

相关问题 更多 >

    热门问题