开发者

python code problem

i have this code:

class Check(webapp.RequestHandler):
  def get(self):
    user = users.get_cu开发者_StackOverflow社区rrent_user()

    be = "SELECT * FROM Benutzer ORDER BY date "
    c = db.GqlQuery(be)

    for x in c:
      if x.benutzer == user:
        s=1
        break
      else:
        s=2
    if s is 0:
      self.redirect('/')

to check whether the user is registered or not. but it gives me an error:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 511, in __call__
    handler.get(*groups)
  File "/Users/zainab_alhaidary/Desktop/الحمد لله/check.py", line 23, in get
    if s is 0:
UnboundLocalError: local variable 's' referenced before assignment

what should i do???


Define s before to assign it a value (also, change the test on s):

user = users.get_current_user()

be = "SELECT * FROM Benutzer ORDER BY date "

c = db.GqlQuery(be)

s=0    # <- init s here

for x in c:
  if x.benutzer == user:
    s=1
    break
  else:
    s=2
if s == 0:    # <- change test on s
  self.redirect('/')


Why exactly are you loading all users, then looping through them, just to find one? Use a where clause:

be = "SELECT * FROM Benutzer WHERE benutzer=:1"
c = db.GqlQuery(be, user)
user_from_db = c.get()
if user_from_db is not None: # found someone
    dostuff()
else:
    self.redirect('/')


You're using 's' before you assign something to it. Add an 's = 0' in the appropriate location.


You want to set s to 0 before the for loop starts. If the query returns zero items, your for loop doesn't loop even once, so s is undefined.

Also, you should use if s == 0: instead of if s is 0:. In CPython, they are both equivalent, but you shouldn't rely on the fact. See: the documentation for PyInt_FromLong and "is" operator behaves unexpectedly with integers.


Your problem is that if c is an empty list then the code in the for loop is never run and s never gets set, hence the error:

UnboundLocalError: local variable 's' referenced before assignment

What the error is telling you that you're referencing - i.e. using - s before it has any value - i.e. before a value has been assigned to it.

To fix this you just ensure s always is assigned a value:

s = 0

for x in c:
    if x.benutzer == user:
        s = 1
        break
    else:
        s = 2


In the case that c is empty the if statement in the loop never gets executed

you should set s=0 before the for loop


I don't know why you are doing this, but if I understand your code correctly, you have s=1 when x.benutzer == user, and s=2 otherwise (shouldn't this be s=0 if you are going to check against 0?).

for x in c:
   if x.benutzer == user:
      s=1
      break
   else:
      s=2
if s is 0:
   self.redirect('/')

Anyway, here's my solution:

if not any(x.benutzer == user for x in c):
   self.redirect('/')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜