循环语句

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


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

 

 

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("")

 

转载于:https://www.cnblogs.com/tingtin/p/9851816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值