创新互联python教程:
写一个 Python 程序,用 For 循环和 While 循环打印乘法表,并举例说明。
这个 Python 程序使用 For 循环显示从 8 到 10 的乘法表。
print(" Multiplication Table ")
for i in range(8, 10):
for j in range(1, 11):
print('{0} * {1} = {2}'.format(i, j, i*j))
print('==============')
Python 乘法表输出
Multiplication Table
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
==============
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
9 * 10 = 90
==============
这个 Python 程序允许用户输入任意整数值。接下来, For Loop 中的打印功能将用户输入值的乘法表打印到 10。
num = int(input(" Please Enter any Positive Integer lessthan 10 : "))
print(" Multiplication Table ")
for i in range(num, 10):
for j in range(1, 11):
print('{0} * {1} = {2}'.format(i, j, i*j))
print('==============')
这个 Python 乘法表程序同上。但是这次,我们使用的是 While Loop 。
i = int(input(" Please Enter any Positive Integer lessthan 10 : "))
print(" Multiplication Table ")
while(i <= 10):
j = 1
while(j <= 10):
print('{0} * {1} = {2}'.format(i, j, i*j))
j = j + 1
print('==============')
i = i + 1
Please Enter any Positive Integer lessthan 10 : 8
Multiplication Table
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
==============
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
9 * 10 = 90
==============
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
==============
网页标题:Python程序:打印乘法表
网站地址:http://www.shufengxianlan.com/qtweb/news49/457499.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联