首页 > 代码库 > python_day1作业
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作业
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。