python3判断循环小练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3判断循环小练习相关的知识,希望对你有一定的参考价值。
1、使用while循环输出1 2 3 4 5 6 8 9 10
i = 1 while i <= 10: if i == 7: i += 1 print(end=‘ ‘) continue print(i, end=‘ ‘) i += 1
2、求1-100的所有数的和
s = 0 for i in range(1, 101): s = s + i i += 1 print(s)
3、输出 1-100 内的所有奇数
for i in range(1, 100): if i % 2 == 1: print(i, end=‘ ‘) i += 1
4、输出 1-100 内的所有偶数
for i in range(1, 101): if i % 2 == 0: print(i, end=‘ ‘) i += 1
5、求1-2+3-4+5 ... 99的所有数的和
s = 0 for i in range(1, 100): if i % 2 == 1: s = s + i elif i % 2 == 0: s = s - i i += 1 print(s)
6、用户登陆(三次机会重试)
name = ‘egon‘ passwd = ‘123‘ i = 1 while i <= 3: na = input(‘请输入用户名:‘) pa = input(‘请输入密码:‘) if na == name and pa == passwd: print(‘登录成功!‘) break elif na == name and pa != passwd: print(‘密码错误,还有‘, 3 - i, ‘次机会‘) else: print(‘无此用户,还有‘, 3 - i, ‘次机会‘) i += 1
7、九九乘法表
for i in range(1, 10): for j in range(1, i+1): print(‘%s*%s*=%s‘ % (i, j, i*j), end=‘ ‘) print(‘\n‘)
8、判断登录,按q退出
tag = True while tag: name = input(‘your name:‘) password = input(‘your password:‘) if name == ‘egon‘ and password == ‘123‘: print(‘login successful‘) while tag: res = input(‘cmd>> ‘) if res == ‘q‘: tag = False continue print(‘用户命令是%‘ % res)
以上是关于python3判断循环小练习的主要内容,如果未能解决你的问题,请参考以下文章