• 循环语句


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


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

     

  • 相关阅读:
    [Algo] 306. Check If Linked List Is Palindrome
    [Algo] 42. Partition Linked List
    [Algo] 20. Search In Unknown Sized Sorted Array
    [LC] 1167. Minimum Cost to Connect Sticks
    [LC] 212. Word Search II
    安装oracle developer 10g 时报错,提示must be 5.0, 5.1, 5.2 or 6.1 . Actual 6.2
    APP-INV-05577
    客户化的并发程序出现黄条警告,并无法输出内容
    EBS Concurrent Manager(并发管理器)异常处理[final]【转】
    oracle 通过透明网关gateway 连接sqlserver
  • 原文地址:https://www.cnblogs.com/tingtin/p/9851816.html
Copyright © 2020-2023  润新知