lundi 23 juillet 2018

Slicing the Values of a Dictionary

I'm trying to write a function that takes three parameters: a dictionary, emp_dict, a string, username, and a number emp_no.

The key values in the dicionary store employee number as the key and a string for the employee name. The first three letters or the last three letters of the username must match the first three letters of the employee's name.

How can I compare the string username to each of the dictionary's first three value fields.

Other constraints include:

  • The given employee number exists in the dictionary
  • The username must be at least 4 characters
  • The first three letters or the last three letters of the username must match the first three letters of the employee's name

Here is what I have so far:

def validate_employee(emp_dict, username, emp_no):
if emp_no not in emp_dict:
    return False
elif len(username) < 4:
    return False

This is where I'm having trouble. How can I write the next two elifs?

elif username[0:2}.lower == emp_dict.values():
    return True




emp_dict={1111:'Sim',1234:'James',9999:'Amy',5555:'Jessica'}
username='Jess' 
emp_no=5555
print(validate_employee(emp_dict, username, emp_no))

I apologize if I didn't ask this very well or if I made some pretty rookie mistakes. I'm pretty new to StackOverflow and Python programming in general. I sometimes have some trouble thinking through some problems logically.

Thanks for any help you can offer!

Aucun commentaire:

Enregistrer un commentaire