码迷,mamicode.com
首页 > 其他好文 > 详细

循环 loop

时间:2019-02-21 00:10:48      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:some   zcl   odi   今天   nbsp   tle   bsp   one   put   

今天学习了loop,包括for计数循环和while条件循环

以下是学习代码

打印了9*9乘法口诀表

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 16:09:46 2019

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Wed Feb 20 16:09:46 2019
 4 
 5 @author: ZCL
 6 """
 7 #学习loop
 8 #for计数循环
 9 for looper in [1,2,3,4,5]:
10     print "hello"
11     print looper
12  #左下三角格式输出九九乘法表
13 """
14 for i in range(1,10):
15     for j in range(1,i+1):
16         print j,"*",i,"=",i*j," "
17         不加逗号默认换行
18     print ""
19 """
20 for i in range(1,10):
21     for j in range(1,i+1):
22         print j,"*",i,"=",j*i,"\t",
23     print("")
24 print("")
25 #反着打印
26 for i in range(1,10):
27     for j in range(1,11-i):
28         print j,"*",10-i,"=",(10-i)*j,"\t",
29     print("")
30 #反向计数,计数到i+1
31 for k in range(10,1,-1):
32     print k,"",
33 print "\n"
34 #右上三角格式输出九九乘法表
35 for i in range(1,10):
36 #打印空格
37     for k in range(1,i):
38         print "\t",
39     for j in range(i,10):
40             print i,"*",j,"=",i*j,"\t",
41     print ""
42 print "\n"
43 #遍历字符串列表
44 for cool_boy in ["one","two","three"]:
45     print cool_boy.title(),"is so handsome!"
46 #学习 while 循环
47 print type 3 to continue,anything else to quit
48 Some_input=raw_input()
49 while Some_input==3:
50     print thank you for the 3, very kind of you
51     print type 3 to continue,anything else to quit
52     Some_input=raw_input()
53 print "that‘s not 3, so I‘m quitting now"
54 #学习break and continue
55 #continue:提前跳转,结束此次迭代
56 for i in range(1,10):
57     print
58     print i=,i,"how",
59     if i==3:
60         continue
61     print are you today?
62 #学习break:跳出循环
63 for i in range(1,10):
64     print
65     print i=,i,"how",
66     if i==3:
67         break
68     print are you today?
69 print \n

结果如图:

技术图片

没有截完整。学习到了for循环的range函数的用法,包括反向计数。其次是输出的时候用“””加逗号,实现一行的继续打印,可以留意一下。Python3似乎输出格式不太一样。

明天学习if语句和嵌套。

 

循环 loop

标签:some   zcl   odi   今天   nbsp   tle   bsp   one   put   

原文地址:https://www.cnblogs.com/zcl1997/p/10409968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!