【问题标题】:How to call classes and function in if statement in python 3X如何在python 3X中的if语句中调用类和函数
【发布时间】:2020-05-27 08:30:36
【问题描述】:

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

提前致谢!

【问题讨论】:

  • exec_menu 中,您将choice 设置为A 的值
  • 但如果我删除了choice = "A",我将if语句中的所有单词choice设置为默认值。

标签: python function class if-statement methods


【解决方案1】:

我做了一些修复。试试:

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完成你想做的事。

【讨论】:

  • 哇!这里还有一个问题 ==> user_input = input("Enter:") 我怎样才能返回菜单。谢谢
  • 你可以在while True循环中设置choice = self.menu()
猜你喜欢
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-08
  • 2022-08-19
  • 2020-04-23
  • 2016-10-22
  • 1970-01-01
相关资源
最近更新 更多