简体   繁体   English

将 for 循环与瓶子一起使用

[英]using for loops with bottle

I am currently developing a python web-app based on bottle so what I am trying to do is print the outcome of a for loop I have tried the following我目前正在开发基于Python的web应用程序一瓶所以我试图做的是打印的结果为循环我曾尝试以下

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    for i in range(10):
         return i

but this did not work and i got this from the development server output但这不起作用,我从开发服务器输出中得到了这个

localhost - - [13/Jan/2017 18:11:38] "GET /request HTTP/1.1" 200 0
localhost - - [13/Jan/2017 18:11:40] "GET /favicon.ico HTTP/1.1" 200 0

so i tried this所以我试过这个

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    sumOfValues=0
    for i in range(10):
        sumOfValues+=i
    return sumOfValues

this also did not work and my devlopment server gave me this这也不起作用,我的开发服务器给了我这个

localhost - - [13/Jan/2017 18:15:44] "GET /request HTTP/1.1" 500 746
localhost - - [13/Jan/2017 18:15:46] "GET /favicon.ico HTTP/1.1" 500 750

so how can I do it I tried searching google but nothing came back , thanks in advance那么我该怎么做我尝试搜索谷歌但没有回来,提前致谢

If you return in a function, it immediately ends.如果在函数中return ,它会立即结束。

It looks like you want to do a streaming response of-sorts.看起来您想要进行某种流式响应。 Bottle can do this, but you must yield items . Bottle 可以做到这一点,但你必须yield items

See also:也可以看看:

函数必须返回字符串 - 所以使用return str(sumOfValues)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM