這個問題可能非常愚蠢,但我試圖用字典來迭代並返回結果。我知道如何迭代字典,但我想檢查輸入的密鑰是否存在於字典中,如果值存在或不存在,我希望程序打印。使用IF內部FOR循環的Python
class companyx:
def __init__(self,empid):
self.empid=empid
def employees(self):
employees={1:'Jane',2:'David',3:'Chris',4:'Roger'}
entered=self.empid
for emp in employees :
if emp == entered:
print ('Hi '+employees[emp] +' you are an employee of companyx.com')
print('You dont belong here')
emp=companyx(2)
emp.employees()
當我傳遞一個參數,它是不是在字典中,我想要的功能打印「你不屬於這裏」
不需要'keys()':'如果輸入員工'。 – alecxe