3

I am creating a number guessing game to help myself learn how to use Google's App Engine. I store the current answer as a random number in the datastore. Each time the user guesses I want to pull the answer out in a GqlQuery and compare to the users guess. When I use the GqlQuery to pull the answer it returns 'None', and I don't know why or what to change to get the random number instead.

Here is where I store the answer -

class Answer(db.Model):
    ans = db.IntegerProperty()

Here is how I store the random number when the user creates a new game -

thisanswer =(random.randint(1, 100))
answer = Answer(ans = thisanswer)
answer.put()

And I try to pull the answer out when the user submits a guess so I can compare to their guess like this-

q = db.GqlQuery("SELECT * FROM Answer")
q = q.fetch(1)
for p in q:
    answer = p.ans

So when I try

if guess > answer:
    msg = "Too high!"
elif guess < answer:
    msg = "Too low!"

etc. I get the msg "Too high!" every time. So I changed the "Too high!" msg to this-

msg = "Too high! " + str(answer)

and what I get is-

"Too high! None"

Why is this code returning a value of 'None'? What do you recommend I change to get the random number that I expect to get. I have been following the App Engine Python docs for the datastore but obviously I am overlooking something. Thanks for any insight on this.

  • 1
    Seems odd to reuse q: `q = q.fetch(1)`. Try `for p in q.fetch(1):` instead, omitting the prior statement. – hyperslug Aug 17 '11 at 18:45

1 Answers1

2

It's possible there are no answers in the database. Verify it with:

msg = "Total answers in database: " + str(Answer.all().count())

A more elegant way of getting the first answer in the database would be:

answer = Answer.all().get()
# or (equivalent)
answer = db.GqlQuery("SELECT * FROM Answer").get()

if answer is not None:
  number = answer.ans
else:
  number = None
orip
  • 73,323
  • 21
  • 116
  • 148
  • Ok thanks for the input. I see now that there really are no answers in the database to begin with. This leads me to believe the problem is how I am putting the answer into the database. Do you have any more ideas- this time as to why my code above where I store the random number into the database (the one with answer.put()) might not be written correctly? – stairsflowers Aug 17 '11 at 19:18
  • Try to add an answer and immediately query the DB, e.g. `Answer(ans=17).put()` and in the next line `msg = str(Answer.all().count())`, and see what you get. – orip Aug 17 '11 at 19:21
  • It worked I put a random number in as a new game and got a number back. But when I try to guess a number it still returns None.. – stairsflowers Aug 17 '11 at 19:29
  • So you know the GQL code works, which probably means your original code creating the answer isn't called for some reason. Time to figure out why :) – orip Aug 17 '11 at 19:33
  • Yes sorry.. I tried to vote up but it says vote up requires 15 reputation. I don't have that much yet, but I can do the accept :) – stairsflowers Aug 17 '11 at 19:54