python学习——练习题(8)

"""
题目:输出 9*9 乘法口诀表。
"""


def answer1():
    """
    自己用最普通的双重循环来输出
    :return:
    """
    print("输出一:")
    for i in range(1, 10):
        for j in range(1, i + 1):
            print("%d x %d =%2s" % (i, j, i * j), end="  ")
        print()


answer1()


def answer2():
    """
    while循环,与另一种格式化
    注意print 百分号(%)格式化输出,以%开始为标志设置输出的格式,而fomate格式化是以冒号(:)开始为标志设置字符串格式
    :return:
    """
    print("输出二:")
    i = 1
    while i < 10:
        j = 1
        while j <= i:
            print("{} x {} ={:2}".format(i, j, i * j), end="  ")
            j += 1
        print()
        i += 1


answer2()

  

原文地址:https://www.cnblogs.com/longphui/p/8056235.html