python中while的用法_Python—while循环,内涵五种九九乘法表

while语句

while循环是一个条件循环语句,如果while后的条件为真时,代码块一直循环,直到条件不再为真则结束循环。

while循环的语法如下:

while expression:

suite_to_repeat

下图可简单说明while语句循环的执行过程:

570a179fa34c4e7d830dc392a0286eb4.jpg

回顾:python规定

①任何非数字0和非空对象都为True。

②数字0,空(null)对象和特殊对象None都为False。

③True,False首字母必须大写。

例子:

>>> not 0

True

>>> not 1

False

>>> not []

True

>>> not [0]

False

>>> not [1]

False

>>> not True

False

>>> not False

True

1、while语句用法

(1)一般用法,计数循环。

①正序,样例源文件:

strs = "Hello World."

count = 0

while count <= 11:

print(strs[count],end=' ')

count += 1

print()

print('end')

执行结果:

H e l l o W o r l d .

end

②倒序,样例源文件:

strs = "Hello World."

count = 11

i = 11

while (i <= 11 and i >=0):

print(strs[count-i],end=' ')

i -= 1

#break

print()

print('end')

执行结果:

H e l l o W o r l d .

end

(2)无限循环

①当条件判断为布尔真时,无限循环。

样例源文件:

while True:

print("Hello World.")

执行结果:

Hello World.

Hello World.

Hello World.

Hello World.

Hello World.

Hello World.

……

②当条件不变时,陷入死循环。

样例源文件:

num = 1

while num == 1:

print("Hello World.")

执行结果:

Hello World.

Hello World.

Hello World.

Hello World.

Hello World.

Hello World.

……

(3)while…else…,else后的代码块为条件假时执行。

样例代码:

>>> while False:

... print("hello world.")

... else:

... print("nihao python.")

...

nihao python.

2、pass、continue、break在while语句中的用法

(1)pass在while语句和for语句用法相同,仅占位,能避免格式错误。

样例源文件:

strs = "Hello World."

count = 0

while count <= 11:

print(strs[count],end=' ')

print("此时索引为{}".format(count))

pass

count += 1

print()

print('end')

执行结果:

H 此时索引为0

e 此时索引为1

l 此时索引为2

l 此时索引为3

o 此时索引为4

此时索引为5

W 此时索引为6

o 此时索引为7

r 此时索引为8

l 此时索引为9

d 此时索引为10

. 此时索引为11

end

(2)continue在while语句中表示结束本次循环并开始下一次循环,直到条件为False时,结束循环。

①样例源代码:

strs = "nihao python."

count = 0

while count <= 12:

print(strs[count],end=' ')

print("此时索引为{}".format(count))

continue

count += 1

print()

print('end')

执行结果:

n 此时索引为0

n 此时索引为0

n 此时索引为0

n 此时索引为0

n 此时索引为0

n 此时索引为0

……

循环每次到continue即开始下一次循环,使得每次count一直为0,陷入了死循环。

②样例源代码:

strs = "nihao python."

count = 0

while count <= 12:

print(strs[count],end=' ')

count += 1

continue

print("此时索引为{}".format(count-1))

print()

print('end')

执行结果:

n i h a o p y t h o n .

end

(3)break在while语句中表示结束循环。

样例源代码:

strs = "BaDianGangHuo."

count = 0

while count <= 13:

print(strs[count],end=' ')

print("此时索引为{}".format(count))

break

count += 1

print()

print('end')

执行结果:

B 此时索引为0

end

3、while语句编写九成九乘法表

①我最初学的乘法表,源代码:

i = 1

while i <= 9:

j = 1

while j <= i:

#print("{}*{}={}".format(j,i,i*j),end = ' ')

print("%d*%d=%-2d"%(j,i,i*j),end = ' ') # '-2'代表靠左对齐,两个占位符

j += 1

print()

i += 1

执行结果:

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

②把①上下颠倒的乘法表,源代码:

i = 9

while i >= 1:

j = 1

while j <= i:

print("%d*%d=%2d"%(j,i,i*j),end = ' ')

j += 1

print()

i -= 1

执行结果:

1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25

1*4= 4 2*4= 8 3*4=12 4*4=16

1*3= 3 2*3= 6 3*3= 9

1*2= 2 2*2= 4

1*1= 1

③把②左右颠倒的乘法表,源代码:

i = 9

while i >= 1:

k = 9

while (k-i) > 0 :

print(' ',end = '') #算上空格一组式子占用7个空格,比如8*8=64 所以空7个格

k -= 1

j = i

while j >= 1:

print("%d*%d=%2d"%(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*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1= 8

7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1= 7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1= 6

5*5=25 5*4=20 5*3=15 5*2=10 5*1= 5

4*4=16 4*3=12 4*2= 8 4*1= 4

3*3= 9 3*2= 6 3*1= 3

2*2= 4 2*1= 2

1*1= 1

④从大到小排序的乘法表,源代码:

i = 9

while i >= 1:

j = 9

while j>= i:

print("%d*%d=%2d"%(j,i,i*j),end = ' ')

j -= 1

print()

i -= 1

执行结果:

9*9=81

9*8=72 8*8=64

9*7=63 8*7=56 7*7=49

9*6=54 8*6=48 7*6=42 6*6=36

9*5=45 8*5=40 7*5=35 6*5=30 5*5=25

9*4=36 8*4=32 7*4=28 6*4=24 5*4=20 4*4=16

9*3=27 8*3=24 7*3=21 6*3=18 5*3=15 4*3=12 3*3= 9

9*2=18 8*2=16 7*2=14 6*2=12 5*2=10 4*2= 8 3*2= 6 2*2= 4

9*1= 9 8*1= 8 7*1= 7 6*1= 6 5*1= 5 4*1= 4 3*1= 3 2*1= 2 1*1= 1

⑤右对齐的乘法表,源代码:

i = 1

while i <= 9:

k = 1

while k <= (9 - i): #判断应该用多少个空格来填充前面的空白

print(' ',end = '') #算上空格一组式子占用7个空格,比如8*8=64 所以空7个格

k += 1

j = 1

while j <= i:

print("%d*%d=%-2d"%(j,i,i*j),end = ' ')

j += 1

print()

i += 1

执行结果:

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

想看最新更新,请关注我的公众号

91c21c610f5d4b6ea9021b139592dbb4.jpg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Sure, here's the code to print the multiplication table using while loop in Python: ``` i = 1 while i <= 9: j = 1 while j <= i: print(f"{j} * {i} = {i*j}", end="\t") j += 1 print() i += 1 ``` This will output the multiplication table from 1 to 9. ### 回答2: Python使用while循环打印九九乘法表的方法如下: ```python i = 1 # 初始化外层循环计数器i while i <= 9: # 外层循环条件,控制行数 j = 1 # 初始化内层循环计数器j while j <= i: # 内层循环条件,控制每行的列数 print(f"{j} * {i} = {i * j}\t", end="") # 打印乘法表达式 j += 1 # 内层循环计数器增加1 print() # 打印换行 i += 1 # 外层循环计数器增加1 ``` 首先,我们定义外层循环计数器i,并初始化为1。接下来,使用while循环来控制行数,当i小于等于9时,进入外层循环。 在内层循环,我们定义内层循环计数器j,并初始化为1。使用while循环来控制每行的列数,当j小于等于i时,执行循环。 在内部循环,我们使用print函数打印乘法表达式。采用f字符串格式化,输出每行的乘法表达式,其{ }的i和j分别表示行数和列数,i * j表示相乘的结果。通过end参数将每个表达式打印在同一行,使用制表符`\t`进行分隔。 然后,内层循环计数器j增加1,继续执行内层循环,直到列数j等于行数i为止。 最后,我们使用print函数打印换行符,使每一行的表达式输出后换行。 最后,外层循环计数器i增加1,继续执行外层循环,直到行数i大于9为止。 运行以上代码,即可打印出九九乘法表。 ### 回答3: 九九乘法表是一种常见的数学运算表格,用来记录1到9的乘积结果。为了使用Python语言打印九九乘法表,我们可以使用while循环结构来完成。 下面是一种实现九九乘法表Python代码: ``` i = 1 while i <= 9: j = 1 while j <= i: print("%d * %d = %d" % (j, i, i * j), end="\t") j += 1 print() i += 1 ``` 在上述代码,我们使用了两个while循环嵌套的结构。外层循环的条件是`i <= 9`,表示从1到9的行数;内层循环的条件是`j <= i`,表示在每一行打印列数少于等于行数的乘法表达式。 在内层循环,我们使用了`print("%d * %d = %d" % (j, i, i * j), end="\t")`来打印乘法表达式,并通过`end="\t"`参数指定以制表符作为表达式之间的间隔,使得打印结果按表格形式输出。 最后,通过`print()`函数打印空行,实现每行打印完毕后换行。 运行上述代码,即可在控制台输出九九乘法表的结果,具体如下所示: ``` 1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 ``` 以上就是使用Pythonwhile循环打印九九乘法表的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值