zoukankan      html  css  js  c++  java
  • Python中的while循环中的小应用

    while循环中的十行十列

     

    两个while输出十行十列的 *
    i = 0
    while i < 10:
        j = 0
        while j < 10:
            print('*',end = '')
            j += 1
        print(' ',end = '')  #换行之后继续在这行输出
        i += 1

     下图为以上代码运行结果:

    一个while循环写出的十行十列的 *

    i = 0

    while i < 100:

        print('*',end = "" )

        if i % 10 == 9:    #判断什么时候换行

            print()             #print() :换行

        i += 1

     下图为以上代码运行结果:

     两个while隔行换样
    i = 0
    while i < 10:
        j = 0
        while j < 10:
            if i % 2 == 0:    #判断是奇数行还是偶数行
                print("@",end ='')
            else:
                print('#',end = '')
            j += 1
        print()
        i += 1

    下图为以上代码运行结果:

    一个while循环的隔行变样
    i = 0
    while i < 100:

        if (i // 10) % 2 == 0:     #判断是奇数行还是偶数行
            print('@',end = '')
        else:
            print('#',end = '')
        if i % 10 == 9:
            print()
        i += 1

     下图为以上代码运行结果:

    两个while隔列变样

    i = 0

    while i < 10:

        j = 0

        while j < 10:

            if j % 2 == 0:       #判断是奇数列还是偶数列

                print('*',end = '')

            else:

                print('#',end = '')

            j += 1

     print()

        i += 1

      下图为以上代码运行结果:

    一个while循环的隔列变样
    i = 0
    while i < 100:
        if i % 2 == 0:      #判断是奇数列还是偶数列
            print('*',end = '')
        else:
            print('#',end = '')
        if i % 10 == 9:
            print()
        i += 1

      下图为以上代码运行结果:


    以上是本人在学习Python中的小例子,希望能帮到和我一样初学Python的人,如果有人有更好的建议,希望尽情留言!!

     

     

  • 相关阅读:
    61. Rotate List
    60. Permutation Sequence
    59. Spiral Matrix II ***
    58. Length of Last Word
    57. Insert Interval
    328. Odd Even Linked List
    237. Delete Node in a Linked List
    关于找List的中间Node
    234. Palindrome Linked List
    203. Remove Linked List Elements *
  • 原文地址:https://www.cnblogs.com/anzai/p/7822371.html
Copyright © 2011-2022 走看看