等效于a'而true'烧瓶中的循环:如何做?
我晚上的爱好是为我女儿编写地形测验。利用网络和 Stack Overflow 资源,我在 https://ac1976.pythonanywhere.com/ 上运行了一个 API api/wereld 点击它,服务器将响应一个由随机国家/首都组合组成的 JSON 对象,以及首都的“错误”答案列表。错误答案取决于距(正确)首都的距离,并且也是随机的。因此,东京不会出现作为比利时首都的答案,但巴黎和伦敦可能会出现。
主题:现在构建一个 Flask 应用程序,从上述 API 获取响应,并创建一个多项选择测验游戏,询问随机国家的首都,并为用户提供 4 个多项选择选项。
我完成所有这些工作的主要路线如下:
@app.route('/api/continent/', methods=['GET', 'POST'])
def quiz():
if request.method == "GET":
quiz = Game('wereld')
country = quiz.country
answers = quiz.answers
session['capital'] = quiz.capital
return render_template('quiz.html', a1=answers[0], a2=answers[1], a3=answers[2], a4=answers[3], country=country)
else:
answer = request.form['subject']
if answer == session.get('capital'):
return render_template('antwoord.html', answer="Jaaaaa...")
else:
return render_template('antwoord.html', answer="Neee..")
因此,在最初点击路线时,服务器初始化 Game 类,它本质上是一个包装器,用于捕获来自 API 的响应,并使用每个的方法来打包响应我们需要的数据点:国家、答案和资本。这些测验项目存储在类似命名的变量“国家/地区”、“答案”和“资本”中。 HTML 模板文件中使用国家/地区(以问题的形式)和答案(作为 4 个单独的按钮),在点击路线时显示。
如果用户按下四个按钮之一,路由逻辑的第二部分将确定答案是正确还是错误,并发送回一个新的 html 视图,告诉用户答案是正确的......还是错误的。
好的。一切都有道理,对吧?
这是我的问题。如何在 1 秒后自动返回到“if 循环的顶部”,以便代码自动从 API 重新获取新响应,以进行另一轮游戏?我尝试在 if / else 循环之上放置一个简单的“While True:”,这适用于我的本测验的原始终端版本...但 Flask 没有它。我明白为什么(它需要一个“GET”)但是......如何引起一个新的?
感谢您的想法/指导/为我指明了正确的方向
阿里
Spending my evenings hobby coding a topography quiz for my daughter. Leveraging the web and Stack Overflow resources, I got an API running on https://ac1976.pythonanywhere.com/api/wereld Hit it and the server will respond with a JSON object comprised of a random country / capital combination, together with a list of 'wrong' answers for the capital. The wrong answers are based on distance to the (correct) capital and are also randomized. So Tokyo won't show up as an answer for the capital of Belgium, but Paris and London may.
On topic: now building a flask app that fetches a response from the aforementioned API, and creates a multiple choice quiz game asking for the capital of the random country, and giving the user 4 multiple choice options.
My main route to do all of this works, and looks like this:
@app.route('/api/continent/', methods=['GET', 'POST'])
def quiz():
if request.method == "GET":
quiz = Game('wereld')
country = quiz.country
answers = quiz.answers
session['capital'] = quiz.capital
return render_template('quiz.html', a1=answers[0], a2=answers[1], a3=answers[2], a4=answers[3], country=country)
else:
answer = request.form['subject']
if answer == session.get('capital'):
return render_template('antwoord.html', answer="Jaaaaa...")
else:
return render_template('antwoord.html', answer="Neee..")
So on hitting the route initially, the server initializes the class Game, which is essentially a wrapper to catch the response from the API and parcel the response with methods for each of the data points we need: country, answers, and capital. These quiz items are stored in similarly named variables country, answers and capital. Country (in the form of a question) and answers (as 4 separate buttons) are used in the HTML template file, presented on hitting the route.
If the user presses one of the four buttons, the second part of the route logic figures out whether the answer was right, or wrong, and sends back a new html view telling the user the answer was right....or wrong.
Okay. All makes sense, right?
Here's my question. How do I autodirect, after say 1 second, back to the 'top of the if loop' so that the code auto refetches a new response from the API, to play another round? I tried to put a simple "While True:" on top of the if / else loop, which works my original terminal version of this quiz...but Flask is not having it. I understand why (it needs a 'GET') but....how to cause a new one?
thanks for your thoughts / guidance / pointing me in the right direction
Arie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTML 页面标题中的元刷新标记怎么样。
定义的时间到期后,您将被重定向到指定的 URL。
然后它看起来像这样。
如果您想使用 JavaScript,也可以使用带有超时的变体。
How about a meta refresh tag within the header of the HTML page.
After the defined time has expired, you will be redirected to the specified URL.
It would then look something like this.
If you want to use JavaScript, a variant with a timeout would also be possible.