• 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的人,如果有人有更好的建议,希望尽情留言!!

     

     

  • 相关阅读:
    [LeetCode] Implement Stack using Queues
    [LeetCode] String Compression
    [国嵌攻略][060][LCD工作原理解析]
    [国嵌攻略][059][2440-DMA程序设计]
    [国嵌攻略][057][串口控制台建立]
    [国嵌攻略][056][串口驱动程序设计]
    [国嵌攻略][054][NandFlash驱动设计_写]
    [问题笔记][指针相加翻译成汇编右移2位]
    [国嵌攻略][053][6410和210按键中断编程]
    [国嵌攻略][050][2440按键中断编程]
  • 原文地址:https://www.cnblogs.com/anzai/p/7822371.html
Copyright © 2020-2023  润新知