循环语句

复制代码
//缩进==放入循环体内部,因此语句的缩进很重要


1
while ans < 5: 2 print("sger") 3 cnt+=1#循环体内部。5次 4 ans +=2#循环体内部,因为缩进了(易错)。3次 5 6 7 8 9 10 11 #只会打印一个0 12 while cnt <5: 13 if cnt%2==0: 14 print(cnt) 15 cnt+=1#是if 的语句 16 17 18 19 #0,2,4 20 while cnt <5: 21 if cnt%2==0: 22 print(cnt) 23 cnt+=1#是 while 的语句 24
复制代码

 

复制代码
>>> for i in range(3,-1,-1):
... print(i); 注意缩进
  File "<stdin>", line 2
    print(i);
        ^
IndentationError: expected an indented block

>>> for i in range(3,-1,-1):
...  print(i)
...
3
2
1
0
复制代码

 

 

复制代码
import math
ch = ""
while ch != "q":
    a = float(input("a: "))
    b = float(input("b: "))
    c = float(input("c: "))
    if a!=0:
        d = b**2-4*a*c
        if d<0:
            print ("no solution!")
        elif d==0:
            s = -b/(2*a)
            print("s: ",s)
        else:
            r = math.sqrt(d)
            s1 = (-b+r)/(2*a)
            s2 = (-b-r)/(2*a)
            print ("Two  distinct solutions are : ",s1,s2)
    ch = input("Quit? ")#缩进要缩对,在while循环体内
复制代码

 

复制代码
# break   and    continue


#break  结束当前循环体
cnt = 0
while cnt < 5:
    if cnt > 2:
        break
    print("okokok!")
    cnt+=1

okokok!
okokok!
okokok!

#continue  结束当次循环
cnt = 0
while cnt < 5:
    cnt+=1
    if cnt > 2:
        continue
    print("okokok!")
    


okokok!
okokok!
复制代码

 

 

1 s = 0
2 for  i in range(11): #range  生成0,……,10的序列
3  s += i#同样要注意缩进
4 print ("s= :",s)

 

 

 

 

 

1 import math
2 e = 1
3 for i in range(1,10):
4      e+=1/math.factorial(i)
5 print(e)

 

1 import math
2 e = 1
3 fac = 1
4 for i in range(1,10):
5      fac*=i
6      e+=1/fac
7 print(e)

 

 

1 n = 6
2 while n!= 1:
3     if n%2 ==0:
4        n /=2
5     else:
6        n = n*3+1
7     print ("n = :{:.0f}".format(n))
#注意下输出的格式
print("每输出十个数字换行,共计输出100个:")
for num in range(1,100):#循环一百次
    print("%3d" % num, end=" ")#不换行输出
    if(num % 10 == 0):
        print("")#换行输出

1 for i in range(1,10):
2     for j in range(1,10):
3         print("%3d"  % (i*j), end = " ")
4     print("")

 

 

复制代码
s = ('acf','af',133)
for i in s :
    print(i,end=',')
acf,af,133,
>>> 
s = "python"
while s!='':
    for c in s:
        if c == 't':
            break#一个break只能跳出一个循环
        print(c,end='')
    print('\n')
    s = s[:-1]

py

py

py

py

py

p


s = "python"
for i in s:
    print(i)
else:#当循环没有break语句退出时,执行else语句块。else 语句块作为正常完成循环的奖励
    print('不是o')
p
y
t
h
o
n
不是o
复制代码

 无限循环用ctrl+c 停止

 

复制代码
#水仙花数
s = ''
for i in range(100,1000):
    sum = 0
    for j in str(i):
        sum+=pow(eval(j),3)
    if sum==i:
        s+="{},".format(i)
print(s[0:-1])
复制代码
字典循环
dict= ['a','b','c'] for i ,val in enumerate(dict): print(i,':',val) 0 : a 1 : b 2 : c

 

 

 

posted on   cltt  阅读(166)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示