【问题标题】:How to use return without ending the for loop in python?如何在不结束python中的for循环的情况下使用return?
【发布时间】:2020-11-02 06:54:00
【问题描述】:

我正在创建一个关于考试系统的项目。 在这里,我有一个名为 questionpapers 的数据库 (db3),其中包含 6 个表格形式的试卷。 我想在我的网页中以超链接的形式返回这些论文的名称。

这是我的python代码:


@app.route('/qsetdisplay',methods=['GET','POST'])
def qset_display():
    if request.method == 'GET':
        print("found template")
        cursor4 = db3.cursor(pymysql.cursors.DictCursor)
        cursor4.execute("show tables")
        papers = cursor4.fetchall()
        print("papers:",papers)
        names = []
        for p in papers:
            print("for p in papers:")
            print("p:",p)
            value = list(p.values())
            print("value:",value)
            names.append(value)
        print("names:",names)
        l = len(names)
        for i in range(l):
            print(names[i][0])
            return(names[i][0])
        return "THESE ARE THE AVAILABLE PAPERS"
    return render_template('profile.html')



if __name__ == '__main__':
    app.run(debug=True)

return 语句只返回第一个元素并结束循环。

这是我的编译器输出:

found template
papers: [{'Tables_in_questionpapers': 'pcdata'}, {'Tables_in_questionpapers': 'physics1'}, {'Tables_in_questionpapers': 'physics_test'}, {'Tables_in_questionpapers': 'sample'}, {'Tables_in_questionpapers': 'sample1'}, {'Tables_in_questionpapers': 'samplea'}]
for p in papers:
p: {'Tables_in_questionpapers': 'pcdata'}
value: ['pcdata']
for p in papers:
p: {'Tables_in_questionpapers': 'physics1'}
value: ['physics1']
for p in papers:
p: {'Tables_in_questionpapers': 'physics_test'}
value: ['physics_test']
for p in papers:
p: {'Tables_in_questionpapers': 'sample'}
value: ['sample']
for p in papers:
p: {'Tables_in_questionpapers': 'sample1'}
value: ['sample1']
for p in papers:
p: {'Tables_in_questionpapers': 'samplea'}
value: ['samplea']
names: [['pcdata'], ['physics1'], ['physics_test'], ['sample'], ['sample1'], ['samplea']]
pcdata

我尝试了收益,但没有成功。 我正在使用 flask 和 pymysql 包。

我怎样才能crct这个代码?

还有没有办法以超链接的形式返回元素?

我想在我的网页中返回所有6个试卷名称

【问题讨论】:

  • 嗯,这就是return 所做的——它完成执行并返回值。你想得到什么作为代码的输出?

标签: python mysql flask return


【解决方案1】:

您应该查看 jinja2,您只需将变量传递到您的 html 代码中,在 html 代码中,您可以执行 for 循环并创建超链接

@app.route('/qsetdisplay',methods=['GET','POST'])
def qset_display():
    if request.method == 'GET':
        print("found template")
        cursor4 = db3.cursor(pymysql.cursors.DictCursor)
        cursor4.execute("show tables")
        papers = cursor4.fetchall()
        print("papers:",papers)
        names = []
        for p in papers:
            print("for p in papers:")
            print("p:",p)
            value = list(p.values())
            print("value:",value)
            names.append(value)
        print("names:",names)
        l = len(names)
        for i in range(l):
            print(names[i][0])
            return(names[i][0])
        #you should delete this line, it's no use
    return render_template('profile.html', names=names)

在您的 html 代码中

{% for name in names %}
<a href="{{ url_for('name') }}">{{ names }}</a>
{% endfor %}

【讨论】:

  • 谢谢你!!你的回答很有帮助
  • 我只想知道 names=names 在代码中的用途。
  • 第一个names 表示jinja2 中的变量名,第二个names 表示python 中的变量名。你明白吗?
  • 感谢您的帮助,先生!
猜你喜欢
  • 1970-01-01
  • 2011-02-22
  • 2021-09-06
  • 2017-03-30
  • 2021-11-06
  • 1970-01-01
  • 2018-03-18
  • 2023-02-26
  • 2013-03-23
相关资源
最近更新 更多