【发布时间】: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