欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python语法之流程控制

程序员文章站 2022-05-09 11:37:49
#1、使用while循环输出1 2 3 4 5 6 8 9 10 # count = 0 # while count < 10: # count += 1 # if count == 7: # continue # print(count) #2、求1-100的所有数的和 # sum = 0 # f... ......
#1、使用while循环输出1 2 3 4 5 6  8 9 10
# count = 0
# while count < 10:
#     count += 1
#     if count == 7:
#         continue
#     print(count)


#2、求1-100的所有数的和
# sum = 0
# for i in range(1, 101):
#     sum += i
#     print(sum)


#3、输出1-100内的所有奇数
# for i in range(1, 101, 2):
#     print(i)


#4、输出1-100内的所有偶数
# for i in range(2, 101, 2):
#         print(i)


#5、求1-2+3-4+5...99的所有数的和
# sum = 0
# for    i in range(100):
#     if i % 2 != 0:
#         sum += i
#     else:
#         sum -= i
# print(f"1-2+3-4+5…+99的所有数的和:{sum}")
#
#
# #6、猜年龄游戏
# age = '18'
# tag = true
# count = 0
# while tag:
#     guess_age = input('请输入年龄:').strip()
#     # guess_age = int(guess_age)
#     if guess_age == age:
#         print('猜对了')
#         break
#     else:
#         print('猜错了')
#         count += 1
#     if count % 3 == 0:
#         answer = input('继续猜请输入y或y,退出请输入n或n:').strip()
#         if answer not in ['y', 'y']:
#             tag = false



#7、运用所学知识,打印以下图案
#     *
#    ***
#   *****
#  *******
# *********

for i in range(1, 6):
    print(' '*(5-i) + '*'*(2*i-1) + ' '*(5-i))

for i in range(1, 10, 2):
    print(("*" * i).center(9))