Flask013_ for 循环语句

Posted jason2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask013_ for 循环语句相关的知识,希望对你有一定的参考价值。

  • 调用
 @app.route(\'/for\')
 def for_statement():
     books = [
         \'title\': \'三国演义\',
         \'author\': \'罗贯中\',
         \'price\': 100
     ,
         
             \'title\': \'水浒传\',
             \'author\': \'施耐庵\',
             \'price\': 99
         ,
         
             \'title\': \'红楼梦\',
             \'author\': \'曹雪芹\',
             \'price\': 101
         ,
         
             \'title\': \'西游记\',
             \'author\': \'吴承恩\',
             \'price\': 102
         
     ]
 
     return render_template(\'for.html\', books=books)
  • for.html
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>for 循环</title>
     <style>
         table 
             border-collapse: collapse;
             text-align: center;
         
 
         th, td 
             padding-left: 20px;
             padding-right: 20px;
             height: 30px;
         
 
         table, th, td 
             border: 1px solid black;
         
 
         .odd 
             background-color: aliceblue;
         
     </style>
 </head>
 <body>
 <table>
     <thead>
     <tr>
         <th>序号</th>
         <th>书名</th>
         <th>作者</th>
         <th>价格</th>
         <th>是否变化</th>
     </tr>
     </thead>
     <tbody>
     % for book in books %
         <tr class=" loop.cycle(\'odd\',\'even\') ">
             <td> loop.index </td>
             <td> book.title </td>
             <td> book.author </td>
             <td> book.price </td>
             <td> loop.changed(book.price) </td>
         </tr>
     % else %
         <tr>
             <td colspan="4" style="text-align: center">无数据</td>
         </tr>
     % endfor %
     </tbody>
 </table>
 </body>
 </html>
  • 效果

 

for循环语句_基础

最简单的for循环语句

#!/bin/bash
for x in one two three four
do
echo number: $x
done

使用文件作为循环的参数
for i in /root/find/*
do
# echo $(basename $i) is a file living in /root/find
# echo $i
# echo $(basename $i)
echo `basename $i`
done

用seq的方式传递参数
for j in $(seq 1 5)
#for j in `seq 1 5`
do
echo $j
done

使用c语言的方式传递参数

for ((i=1;i<=5;i++))
do
echo "i=$i"
done

 

与while循环语句对比

num=1
while [ $num -le 10 ]
do
echo $num
num=$(($num + 1))
done

 

#!/bin/bash
ip=1
while [ $ip -lt 256 ]
do
ping 192.168.0.$ip -c 2 | grep -q "ttl=" #&& echo "192.168.0.$ip yes" || echo "192.168.0.$ip no"
if [ $? -eq 0 ];then
echo "192.168.0.$ip yes"
else
echo "192.168.0.$ip no"
fi

#ip=$(($ip + 1))
ip=`expr "$ip" "+" "1"`
done

 

#!/bin/bash
network=192.168.0.
IP=1
:>IP_use
:>IP_idle
:>ping_action
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
for i in $(seq 1 10)
do
ping $network$i -c 2 | grep -q "ttl="
if [ $? -eq 0 ]
then
echo "$network$i yes"
else
echo "$network$i no"
fi
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行完成......"

 

以上是关于Flask013_ for 循环语句的主要内容,如果未能解决你的问题,请参考以下文章

python013 Python3 循环语句

流程控制 for标签和if标签

flask 模版if 语句和for语句

JS基础_嵌套的for循环

00012_循环语句for

vb6.0 循环语句 退出循环