标签:lin 居中 元素 基础 执行顺序 包括 aaaaa bin 打印
作用: 根据一定的条件,重复的执行一个或多个语句
两种循环语句:
while 语句
for 语句
while 语句:
语法:
while 真值表达式:
语句1
。。。
else:
语句2
。。。
语法说明: else子句可以省略
执行顺序: (1) 先判断真值表达式是否为True
(2)如果第1步为True,则执行语句1后跳到第1步,否则跳到第3步;
(3)执行else 子句
(4)结束 while 语句的执行
>>> n = 1 >>> while n <= 10 : print(n) n += 1 1 2 3 4 5 6 7 8 9 10 >>>
练习1 :用字符串 * 运算符打印三角形;(要求输入一个整数,此整数代表三角形离左侧的字符串* 的距离)
n = int(input("请输入一个整数: ")) left = ‘ ‘ * n print(left + ‘ *‘) print(left + ‘ ***‘) print(left + ‘ *****‘) print(left + ‘*******‘) >>> ================================ RESTART ================================ >>> 请输入一个整数: 3 * *** ***** ******* >>> ================================ RESTART ================================ >>> 请输入一个整数: 0 * *** ***** ******* >>>
练习2 输入三行文字,让三行文字在一个方框内居中显示(输入不要输入中文);
1 #!/usr/bin/python 2 3 #输入3行文字 4 line1 = input("请输入第一行字符:") 5 line2 = input("请输入第二行字符:") 6 line3 = input("请输入第三行字符:") 7 8 #比较出3行中最大的一行字符长度 9 m = max(len(line1), len(line2), len(line3)) 10 11 #使3行输出的文字居中 12 print(‘+‘ + ‘-‘ * ( m + 2 ) + ‘+‘) 13 print(‘| ‘ + line1.center(m) + ‘ |‘) 14 print(‘| ‘ + line2.center(m) + ‘ |‘) 15 print(‘| ‘ + line3.center(m) + ‘ |‘) 16 print(‘+‘ + ‘-‘ * ( m + 2 ) + ‘+‘) 17 >>> ================================ RESTART ================================ 18 >>> 19 请输入第一行字符:aaaaaaaaaaaaa 20 请输入第二行字符:aaaaaaaa 21 请输入第三行字符:aaaaaaaaaa 22 +---------------+ 23 | aaaaaaaaaaaaa | 24 | aaaaaaaa | 25 | aaaaaaaaaa | 26 +---------------+ 27 >>>
while 语句嵌套
语法:
while a > b:
while b > c:
......
else:
.......
1 >>> i = 1 2 >>> while i < 10: 3 j = 1 4 while j < 10: 5 print("i =", i, "j =", j) 6 j += 1 7 i += 1 8 9 10 i = 1 j = 1 11 i = 1 j = 2 12 i = 1 j = 3 13 i = 1 j = 4 14 i = 1 j = 5 15 i = 1 j = 6 16 i = 1 j = 7 17 i = 1 j = 8 18 i = 1 j = 9 19 i = 2 j = 1 20 i = 2 j = 2 21 i = 2 j = 3 22 i = 2 j = 4 23 i = 2 j = 5 24 i = 2 j = 6 25 i = 2 j = 7 26 i = 2 j = 8 27 i = 2 j = 9 28 i = 3 j = 1 29 i = 3 j = 2 30 i = 3 j = 3 31 i = 3 j = 4 32 i = 3 j = 5 33 i = 3 j = 6 34 i = 3 j = 7 35 i = 3 j = 8 36 i = 3 j = 9 37 i = 4 j = 1 38 i = 4 j = 2 39 i = 4 j = 3 40 i = 4 j = 4 41 i = 4 j = 5 42 i = 4 j = 6 43 i = 4 j = 7 44 i = 4 j = 8 45 i = 4 j = 9 46 i = 5 j = 1 47 i = 5 j = 2 48 i = 5 j = 3 49 i = 5 j = 4 50 i = 5 j = 5 51 i = 5 j = 6 52 i = 5 j = 7 53 i = 5 j = 8 54 i = 5 j = 9 55 i = 6 j = 1 56 i = 6 j = 2 57 i = 6 j = 3 58 i = 6 j = 4 59 i = 6 j = 5 60 i = 6 j = 6 61 i = 6 j = 7 62 i = 6 j = 8 63 i = 6 j = 9 64 i = 7 j = 1 65 i = 7 j = 2 66 i = 7 j = 3 67 i = 7 j = 4 68 i = 7 j = 5 69 i = 7 j = 6 70 i = 7 j = 7 71 i = 7 j = 8 72 i = 7 j = 9 73 i = 8 j = 1 74 i = 8 j = 2 75 i = 8 j = 3 76 i = 8 j = 4 77 i = 8 j = 5 78 i = 8 j = 6 79 i = 8 j = 7 80 i = 8 j = 8 81 i = 8 j = 9 82 i = 9 j = 1 83 i = 9 j = 2 84 i = 9 j = 3 85 i = 9 j = 4 86 i = 9 j = 5 87 i = 9 j = 6 88 i = 9 j = 7 89 i = 9 j = 8 90 i = 9 j = 9 91 >>>
for 循环语句
for 语句可以用来遍历或者可迭代对象的每一个元素;
可迭代对象包括:
字符串str 、 列表list 、 元组tuple 、 字典dict 、 集合set 、 固定集合 frozenset 、 迭代器
for 语句的语法:
for 变量列表 in 可迭代对象
语句1
。。。。
else:
语句2
。。。。
说明: else子句部分可以省略 ; 语句1的执行次数与可迭代对象的元素个数有关;
#!/usr/bin/python s = "Hello" for i in s: print("i->>", i) else: print("for 语句结束") print("程序结束") >>> ================================ RESTART ================================ >>> i->> H i->> e i->> l i->> l i->> o for 语句结束 程序结束 >>>
练习:任意输入一个字符串,计算输入的字符‘a’的个数,并打印出来;
标签:lin 居中 元素 基础 执行顺序 包括 aaaaa bin 打印
原文地址:https://www.cnblogs.com/weizitianming/p/8969691.html