0

I have this function in my class:

 def removeUserFromSessionDatabase(self, user):
      if user in self.users:
         for k in self.users.keys():
            if k == user:
               del self.users[k]
               print("Removed")

            else:
               print("user does not exist")
      else:
         print "soemthing"

now I always get error at this last else with message: SyntaxError: invalid syntax where as it should work. users is a dictionary here and there is no other method. Why am I getting this syntax error?

5
  • 1
    What python version? If 3+, this needs to be print() as your other ones are. Commented Sep 8, 2012 at 20:20
  • 1
    why do you do the for k in self.users.keys instead of just self.users.pop(user) in fact you can use pop even if the key doesnt exist so you can get rid of that whole if Commented Sep 8, 2012 at 20:20
  • What version of Python are you using? In Python 3, print is a function: print("Something") Commented Sep 8, 2012 at 20:21
  • 2
    check your code for uncorrect indentation, unclosed parentheses and some forgotten whitespace. Commented Sep 8, 2012 at 20:22
  • @MichaelBerkowski: the syntax error would be on the print line, then, not on the else: statement. Commented Sep 8, 2012 at 20:23

2 Answers 2

3

Your indentation could be incorrect, most likely caused by tabs. Run python -tt scriptname.py to check.

There is otherwise no syntax error in your code that would cause this specific exception, not in the code you've given us.

Sign up to request clarification or add additional context in comments.

4 Comments

oh thanks, that was really the case, i just reset the indentation manually and it works now. Can you tell me a good IDE Please to work with python
@Sara: IDLE is the default IDE for Python. Many Pythonistas work without IDEs, though, preferring an editor in conjunction with IPython.
@Sara: Anything that can be configured to use only spaces instead of tabs is fine.
eclipse + pydev is flat out the best imho ... ninja ide is lightweight and nice ... notepad++(and/or scite) is lightweight and pretty good
3

you can simplify this alot by

def removeUserFromSessionDatabase(self, user):
      return self.users.pop(user,False)

or

def removeUserFromSessionDatabase(self, user):
      if self.users.pop(user,False):
         print "%s was deleted from group"
      else:
         print "%s is not in group"

or

def removeUserFromSessionDatabase(self, user):
      userData = self.users.pop(user,False):
      if userData: 
          #do Something
      else:
          #do something else

or lastly

def removeUserFromSessionDatabase(self, user):
      try:userData = self.users.pop(user):
      except KeyError:
          #user does not exist in dict
          pass
      print "Deleted {0}:{1} From List".format((user,userData))

apparently (per denlan and I believe it) del is fine to use

def removeUserFromSessionDatabase(self, user):
      try:
         del self.users[user]
         print "Removed user"
      except KeyError:  
         print "User does not exist" 


def alt_removeUserFromSessionDatabase(self, user):
      if user in self.users
         del self.users[user]
         print "Removed user"
      else:  
         print "User does not exist" 

9 Comments

Well, except for the output. But yeah, the loop is complete nonsense. As Larry Wall put it: "Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi."
there now it accounts for the output :P
Unfortunately, that's wrong, because .pop (1) returns the values associated with the removed key, not a boolean (and that object may be falsy) and (2) it raises a KeyError if user not in self.users rather than returning something falsy.
there fixed by giving default val (but you are right about the value could be false/none...but in this case it sounds like that would not be a factor
Still, no need to introduce such a subtle assumption, and no need to obfuscate by putting a side effect into a condition. What the heck is wrong with checking if user in self.users and using del self.users[user] in one of the branches?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.