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
while 循环
技术分享
1 for i in range(1,11):
2     if i != 7:
3         print(i)
for 循环

2、1-100的所有数的和

技术分享
1 sum = 0
2 count = 1
3 while count <= 100:
4     sum += count
5     count += 1
6 print(sum)
While 循环
技术分享
1 sum = 0
2 for i in range(1,101):
3     sum += i
4 print(sum)
For 循环

3、输出 1-100 内的所有奇数

技术分享
1 count = 1
2 while count <= 100:
3     if count % 2 == 1:
4         print(count)
5     count += 1
While 循环
技术分享
1 for i in range(1,101):
2     if i % 2 == 1:
3         print(i)
For 循环

4、输出 1-100 内的所有偶数

技术分享
1 count = 1
2 while count <= 100:
3     if count % 2 == 0:
4         print(count)
5     count += 1
While 循环
技术分享
1 for i in range(1,101):
2     if i != 0 and  i % 2 == 0:
3         print(i)
For 循环

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)
While 循环
技术分享
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)
for 循环

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
while 循环
技术分享
 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
另一种While 三次失败 (加上提示,输入次数过多,已锁定 )
技术分享
 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(用户名或密码错误!)
For 循环
技术分享
 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(失败次数过多,已锁定!)
另一种For 三次失败 (加上提示,输入次数过多,已锁定 )

以上是关于python_day1作业的主要内容,如果未能解决你的问题,请参考以下文章

铁乐学python_day01-作业

铁乐学Python_day12_作业

铁乐学python_day09_作业

python_day3作业

python_day3作业

Python_day_01