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

如何在python3x中调用if语句中的类和函数,python,function,class,if-statement,methods,Python,Function,Class,If Statement,Methods,我创建了一个简单的菜单,可以从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')

我创建了一个简单的菜单,可以从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)

提前谢谢

我做了一些修正。尝试:

类自定义错误(异常):
通过
班级员工:
def菜单(自我):
打印('\tA=添加员工记录')
打印('\tB=更新员工记录')
打印('\tC=增加员工工资')
打印('\tD=生成工资单')
打印('\tX=Exit')
返回输入(“\n在此处键入!”)。上限()
def exec_菜单(自我):
choice=self.menu()
尽管如此:
如果选项==“A”:
rec=add_record()#这是来自另一个类
记录员工信息()
elif选项==“B”:
通过
elif选项==“C”:
通过
elif选项==“D”:
通过
elif选项==“X”:
退出()
其他:
打印(“系统现在将退出的值错误!”)
退出()
类添加记录:
def员工信息(自我):
打印(“员工编号长度必须为9个数字”)
打印(“名称和姓氏只能是字符”)
打印(“请在此部门名称上选择:会计、人力资源、营销、财务、MIS、行政”)
尽管如此:
打印(“*”*70)
Employee_empno=int(输入(“为您的员工编号输入唯一编号:”))
Employee_last=str(输入(“输入Lastname:”))
Employee_first=str(输入(“输入名字”))
员工\部门=输入(“输入部门:”)。上限()
员工工资=整数(输入(“输入每小时工资:”)
以open('empRecord.txt','a')作为f:
f、 写入(f“\n{Employee\u empno},{Employee\u last},{Employee\u first},{Employee\u dept},{Employee\u rate};”)
打印(“如果要在信息中添加更多员工,请按1”)
用户输入=输入(“输入:”)
Employee().exec_菜单()

只要完成你想用
用户输入做的事情

我做了一些修复。尝试:

类自定义错误(异常):
通过
班级员工:
def菜单(自我):
打印('\tA=添加员工记录')
打印('\tB=更新员工记录')
打印('\tC=增加员工工资')
打印('\tD=生成工资单')
打印('\tX=Exit')
返回输入(“\n在此处键入!”)。上限()
def exec_菜单(自我):
choice=self.menu()
尽管如此:
如果选项==“A”:
rec=add_record()#这是来自另一个类
记录员工信息()
elif选项==“B”:
通过
elif选项==“C”:
通过
elif选项==“D”:
通过
elif选项==“X”:
退出()
其他:
打印(“系统现在将退出的值错误!”)
退出()
类添加记录:
def员工信息(自我):
打印(“员工编号长度必须为9个数字”)
打印(“名称和姓氏只能是字符”)
打印(“请在此部门名称上选择:会计、人力资源、营销、财务、MIS、行政”)
尽管如此:
打印(“*”*70)
Employee_empno=int(输入(“为您的员工编号输入唯一编号:”))
Employee_last=str(输入(“输入Lastname:”))
Employee_first=str(输入(“输入名字”))
员工\部门=输入(“输入部门:”)。上限()
员工工资=整数(输入(“输入每小时工资:”)
以open('empRecord.txt','a')作为f:
f、 写入(f“\n{Employee\u empno},{Employee\u last},{Employee\u first},{Employee\u dept},{Employee\u rate};”)
打印(“如果要在信息中添加更多员工,请按1”)
用户输入=输入(“输入:”)
Employee().exec_菜单()

只需在
exec\u菜单中完成您想对
user\u input

执行的操作
如果我删除choice=“A”,您将
choice
设置为Abut的值,在if语句中,所有的单词选择都将是红色的,我将其作为默认值。在
exec_菜单中
您正在将
choice
设置为Abut的值。如果我删除choice=“A”,if语句中的所有单词选择都将是红色的,我将其作为默认值。哇!这里还有一个问题==>user\u input=input(“输入:”)我如何回到菜单中。感谢您可以在
中设置
choice=self.menu()
,而设置为True
loopWow!这里还有一个问题==>user\u input=input(“输入:”)我如何回到菜单中。感谢您可以在
中设置
choice=self.menu()
,而在
中设置True
循环