我是靠谱客的博主 兴奋雪糕,这篇文章主要介绍python中while循环语句的练习1. 练习一:用户登录2. 练习二:99乘法表3. 练习三:打印星星,现在分享给大家,希望可以做个参考。

python中while循环语句的练习

  • 1. 练习一:用户登录
  • 2. 练习二:99乘法表
  • 3. 练习三:打印星星

1. 练习一:用户登录

方法一:前面我们使用过for循环实现:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
for i in range(3): #0 1 2 name = input('用户名:') passwd = input('密码:') if name == 'root' and passwd == 'westos': print('登陆成功') # 跳出整个循环,不会再执行后面的内容 break else: print('登陆失败') print('您还剩余%d次机会' %(2-i)) else: print('登陆次数超过三次,请等待100s后再次尝试登陆')

输出结果:
在这里插入图片描述在这里插入图片描述

方法二:while循环

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
i = 0 while i < 3: # 0 1 2 name = input('用户名:') passwd = input('密码:') if name == 'root' and passwd == 'westos': print('登陆成功') # 跳出整个循环 不会再执行后面的内容 break else: print('登陆失败') print('您还剩余%d次机会' %(2-i)) i +=1 else: print('登陆次数超过三次,请等待100s后再试!!!')

输出结果:
在这里插入图片描述在这里插入图片描述

2. 练习二:99乘法表

打印99乘法表

第一种:

复制代码
1
2
3
4
5
6
7
8
9
row = 1 while row <= 9: col = 1 while col <= row: print('%d * %d = %dt' %(row,col,row*col),end='') col += 1 print('') ##手动换行 row += 1

输出结果:
在这里插入图片描述
第二种:

复制代码
1
2
3
4
5
6
7
8
9
row = 1 while row <= 9: col = 9 while col >= row: print('%d * %d = %dt' % (col, row, col * row), end='') col -= 1 print('') row += 1

输出结果:
在这里插入图片描述第三种:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
row = 1 while row <= 9: col = 1 while col < row: print('ttt',end='') col += 1 while col >= row and col <= 9: print('%d * %d = %dt'%(row,col,row*col),end='') col += 1 print('') row += 1

输出结果:
在这里插入图片描述

3. 练习三:打印星星

第一种:

复制代码
1
2
3
4
5
6
7
8
9
row = 1 while row <= 6: col = 1 while col < row: print('*', end='') col += 1 print('') row += 1

输出结果:
在这里插入图片描述第二种:

复制代码
1
2
3
4
5
6
7
8
9
row = 1 while row <= 6: col = 6 while col >= row: print('*', end='') col -= 1 print('') row += 1

输出结果:
在这里插入图片描述
第三种:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
row = 0 while row <= 6: col = 0 while col < row: print('', end='') col += 1 while col >= row and col < 6: print('*', end='') col += 1 print('') row += 1

输出结果:
在这里插入图片描述第四种:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
row = 0 while row <= 6: col = 0 while col < 6 - row: col += 1 print(' ', end='') while col < 6: print('*', end='') col += 1 print('') row += 1

输出结果:
在这里插入图片描述

最后

以上就是兴奋雪糕最近收集整理的关于python中while循环语句的练习1. 练习一:用户登录2. 练习二:99乘法表3. 练习三:打印星星的全部内容,更多相关python中while循环语句的练习1.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部