• Register
0 votes
39 views

I wrote a code to check if a dictonary is empty or not. I wrote this code in python :

 def isEmpty(self, dictionary):
   for element in dictionary:
     if element:
       return True
     return False

 def onMessage(self, socket, message):
  if self.isEmpty(self.users) == False:
     socket.send("No users registered, please register first" \
                 " in order to register into the server")
  else:
     socket.send("ONLINE " + ' ' .join(self.users.keys())) 

Does my isEmpty returns true and is there any problem in my code.

10 7 2
6,060 points

1 Answer

0 votes

Your isEmpty returns True but when you are checking the dictonary is empty or not it returns None Which is not False.

You can try out this code this may help you:

dict = {}

if len(dict) = 0
   print("This directory is empty")
else:
   print("This directory is in use")

you also can use equality test:

def isEmpty(Your directory name):
    """Print true if given dictionary is empty"""
    if my_dict == {}:
        print("Directory is empty !")

Or you can do it by using:

i = { 'x':1, 'y':2, 'z':{}}
bool(i.get('z'))
False
i['z']['e']=1
bool(i.get('z'))
True

 

11 6 4
34,950 points

Related questions

3 votes
1 answer 15 views
15 views
Problem: Hi all, I am trying to figure out the dictionary related problems in python. Dictionary is really fun to learn and work with. Let’s say, I have a dictionary dict = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} If ... needed to check every value inside the dictionary what can I do for that? So, I want to know how to check if a value is in a dictionary python?
asked Mar 24 Gavin 15.3k points
1 vote
1 answer 21 views
21 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3 LizzyM 6.1k points
1 vote
1 answer 27 views
27 views
Problem: I am starting writing code in python. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "Present" else: print "Thanks" If there any other way to check this, please let me know.
asked Mar 10 Gavin 15.3k points
0 votes
1 answer 16 views
16 views
Problem Hi Kodlogs, How to check if a key is in a dictionary python? Please help Thanks
asked Oct 30 hashq 1.1k points
0 votes
1 answer 16 views
16 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14 LizzyM 6.1k points