python_day1作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python_day1作业相关的知识,希望对你有一定的参考价值。
1、使用while循环输出1 2 3 4 5 6 8 9 10
1 count = 1 2 while count <=10: 3 if count != 7: 4 print(count) 5 count += 1
1 for i in range(1,11): 2 if i != 7: 3 print(i)
2、求1-100的所有数的和
1 sum = 0 2 count = 1 3 while count <= 100: 4 sum += count 5 count += 1 6 print(sum)
1 sum = 0 2 for i in range(1,101): 3 sum += i 4 print(sum)
3、输出 1-100 内的所有奇数
1 count = 1 2 while count <= 100: 3 if count % 2 == 1: 4 print(count) 5 count += 1
1 for i in range(1,101): 2 if i % 2 == 1: 3 print(i)
4、输出 1-100 内的所有偶数
1 count = 1 2 while count <= 100: 3 if count % 2 == 0: 4 print(count) 5 count += 1
1 for i in range(1,101): 2 if i != 0 and i % 2 == 0: 3 print(i)
5、求1-2+3-4+5 ... 99的所有数的和
1 count = 1 2 sum = 0 3 while count <= 99: 4 if count % 2 == 1: 5 sum += count 6 count += 1 7 else: 8 sum -= count 9 count += 1 10 print(sum)
1 sum = 0 2 for i in range(1,100): 3 if i % 2 == 1: 4 sum += i 5 else: 6 sum -= i 7 print(sum)
6、用户登陆(三次机会重试)
1 count = 1 2 name = ‘zzn‘ 3 passwd = 123 4 while count <= 3: 5 username = input(‘请输入用户名:‘) 6 password = input(‘请输入密码:‘) 7 password = int(password) 8 if username == name and password == passwd: 9 print(‘登陆成功!‘) 10 break 11 else: 12 print(‘用户名或密码错误!‘) 13 count += 1
1 count = 1 2 name = ‘zzn‘ 3 passwd = 123 4 while True: 5 username = input(‘请输入用户名:‘) 6 password = input(‘请输入密码:‘) 7 password = int(password) 8 if username == name and password == passwd: 9 print(‘登陆成功!‘) 10 break 11 else: 12 print(‘用户名或密码错误!‘) 13 count += 1 14 if count == 4: 15 print(‘失败次数过多,已锁定!‘) 16 break
1 name = ‘zzn‘ 2 passwd = 123 3 for i in range(1,4): 4 username = input(‘请输入用户名:‘) 5 password = input(‘请输入密码:‘) 6 password = int(password) 7 if username == name and password == passwd: 8 print(‘登陆成功!‘) 9 break 10 else: 11 print(‘用户名或密码错误!‘)
1 name = ‘zzn‘ 2 passwd = 123 3 for i in range(1,4): 4 username = input(‘请输入用户名:‘) 5 password = input(‘请输入密码:‘) 6 password = int(password) 7 if username == name and password == passwd: 8 print(‘登陆成功!‘) 9 break 10 else: 11 print(‘用户名或密码错误!‘) 12 if i == 3: 13 print(‘失败次数过多,已锁定!‘)
以上是关于python_day1作业的主要内容,如果未能解决你的问题,请参考以下文章