为什么此 Gql 查询返回 None? (使用 Python 和 App Engine)

发布于 2024-11-30 03:04:50 字数 930 浏览 0 评论 0原文

我正在创建一个猜数字游戏来帮助自己学习如何使用 Google 的 App Engine。我将当前答案作为随机数存储在数据存储中。每次用户猜测时,我都想在 GqlQuery 中提取答案并与用户的猜测进行比较。当我使用 GqlQuery 提取答案时,它返回“无”,并且我不知道为什么或要更改什么来获取随机数。

这是我存储答案的地方 -

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

这是我在用户创建新游戏时存储随机数的方式 -

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

当用户提交猜测时我尝试取出答案,以便我可以像这样与他们的猜测进行比较 -

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

所以当我尝试

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

等等。我收到消息“太高!”每次。所以我把“太高了!”改成了“太高了!”对此的消息

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

- 我得到的是

- “太高!无”

为什么此代码返回值“无”?你建议我改变什么来获得我期望得到的随机数。我一直在关注数据存储区的 App Engine Python 文档,但显然我忽略了一些东西。感谢您对此的任何见解。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-12-07 03:04:50

数据库中可能没有答案。验证它:

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

在数据库中获取第一个答案的更优雅的方法是:

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

if answer is not None:
  number = answer.ans
else:
  number = None

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文