python小程序
- 1-100求和
1 def Sum(x, y):
2 return x+y
3 print reduce(lambda x,y:x+y,range(1,101))
4
5 i = 1
6 j = 0
7 while i < 101:
8 j = i + j
9 i += 1
10 print j
View Code
- 输出1-100之间的奇偶数
1 i = 1
2 a = []
3 while i < 100:
4 if i%2 == 0:
5 a.append(i)
6 i += 1
7 print a
View Code
- 计算1-2+3-4+5-6...99的和
1 i = 1
2 j = 0
3 k = 0
4 while i < 100:
5 if i%2 == 0:
6 j = i + j
7 if i%2 == 1:
8 k = i + k
9 i += 1
10 print k + (-j)
11
12
13 i = 0
14 j = 1
15 while j < 100:
16 temp = j%2
17 if temp == 1:
18 i = i + j
19 else:
20 i = i - j
21 j += 1
22 print i
View Code
- 实现用户登录,超过3次自动退出
1 i = 0
2 name = "nihao"
3 psw = "12345"
4 while True:
5 if i == 3:
6 print "错误3次退出!"
7 break
8 else:
9 a = raw_input("请输出账号:").strip()
10 b = raw_input("请出入密码:").strip()
11 if a == name and b == psw:
12 print "登录成功!"
13 break
14 else:
15 print "账号或密码错误!\n"
16 i += 1
View Code