流程控制示例代码

流程控制

十二生肖

# year = input('请输入一个四位数的年份')
# print(year)
# shunxu = int(year) - 1000
# int(shunxu)
# if shunxu % 12 == 0 :
#     print(year + '年是子鼠年')
# elif shunxu % 12 == 1 :
#     print(year + '年是丑牛年')
# elif shunxu % 12 == 2 :
#     print(year + '年是寅虎年')
# elif shunxu % 12 == 3 :
#     print(year + '年是卯兔年')
# elif shunxu % 12 == 4 :
#     print(year + '年是辰龙年')
# elif shunxu % 12 == 5 :
#     print(year + '年是巳蛇年')
# elif shunxu % 12 == 6 :
#     print(year + '年是午马年')
# elif shunxu % 12 == 7 :
#     print(year + '年是未羊年')
# elif shunxu % 12 == 8 :
#     print(year + '年是申猴年')
# elif shunxu % 12 == 9 :
#     print(year + '年是酉鸡年')
# elif shunxu % 12 == 10 :
#     print(year + '年是戌狗年')
# elif shunxu % 12 == 11 :
#     print(year + '年是亥猪年')


year = int(input('请输入一个四位数的年份'))
varlist = ['申猴','酉鸡','戌狗','亥猪','子鼠','丑牛','寅虎','卯兔','辰龙','巳蛇','午马','未羊']
print(f'{year}年是{varlist[year % 12]}年')

循环结构

# 隔列换色

# num = 0
# while num < 100:
#     if (num % 2 == 0):
#         print('☆', end=" ")
#     else :
#         print('★', end=" ")
#     num += 1
#     if num % 10 == 0:
#         print()

# 隔行换色

# num = 0
# while num < 100:
#     if (num // 10) % 2 == 0:
#         print('☆', end=" ")
#     else :
#         print('★', end=" ")
#     num += 1
#     if num % 10 == 0:
#         print()

# 三角形

# for i in range(0,3):
#     for num in range(0,5):
#         if abs(2 - num) <= i:
#             print('★',end = "")
#         else:
#             print('  ',end="")
#         num += 1
#     print()
#     i += 1

# 菱形

for i in range(0,5):
    for num in range(0,5):
        if abs(2 - num)  <= abs(2 - abs(2 - i)):
            print('★', end="")
        else:
            print('  ', end="")
        num += 1
    print()
    i += 1

打印乘法表

#输出下三角乘法表

for i in range(1,10):
        for j in range(1,i+1):
            print(f'{i} * {j} = {i * j}  ', end="")
    print()
    
'''
1 * 1 = 1  
2 * 1 = 2  2 * 2 = 4  
3 * 1 = 3  3 * 2 = 6  3 * 3 = 9  
4 * 1 = 4  4 * 2 = 8  4 * 3 = 12  4 * 4 = 16  
5 * 1 = 5  5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25  
6 * 1 = 6  6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36  
7 * 1 = 7  7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49  
8 * 1 = 8  8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64  
9 * 1 = 9  9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81  
'''


# 反向输出乘法表

for i in range(9,0,-1):   #另一种写法
    for j in range(i,0,-1):
        print(f'{i} * {j} = {i * j}  ', end="")
    print()

for i in range(1,10):
    for j in range(1,10-i+1):
        print(f'{10-i} * {10-j} = {(10-i) * (10-j)}  ', end="")
    print()
    
# while循环反方向输出九九乘法表
i = 9
while i >= 1 :
    j = i
    while j >= 1 :
        print(f'{i} * {j} = {i * j}  ', end="")
        j -= 1
    print()
    i -= 1
    
'''
9 * 9 = 81  9 * 8 = 72  9 * 7 = 63  9 * 6 = 54  9 * 5 = 45  9 * 4 = 36  9 * 3 = 27  9 * 2 = 18  9 * 1 = 9  
8 * 9 = 72  8 * 8 = 64  8 * 7 = 56  8 * 6 = 48  8 * 5 = 40  8 * 4 = 32  8 * 3 = 24  8 * 2 = 16  
7 * 9 = 63  7 * 8 = 56  7 * 7 = 49  7 * 6 = 42  7 * 5 = 35  7 * 4 = 28  7 * 3 = 21  
6 * 9 = 54  6 * 8 = 48  6 * 7 = 42  6 * 6 = 36  6 * 5 = 30  6 * 4 = 24  
5 * 9 = 45  5 * 8 = 40  5 * 7 = 35  5 * 6 = 30  5 * 5 = 25  
4 * 9 = 36  4 * 8 = 32  4 * 7 = 28  4 * 6 = 24  
3 * 9 = 27  3 * 8 = 24  3 * 7 = 21  
2 * 9 = 18  2 * 8 = 16  
1 * 9 = 9
'''
上一篇:Java实现判断某一年是否是闰年


下一篇:阴阳历自动转换工具函数