为什么此 Gql 查询返回 None? (使用 Python 和 App Engine)
我正在创建一个猜数字游戏来帮助自己学习如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库中可能没有答案。验证它:
在数据库中获取第一个答案的更优雅的方法是:
It's possible there are no answers in the database. Verify it with:
A more elegant way of getting the first answer in the database would be: