python第一次作业
# 1、使用while循环输入 1 2 3 4 5 6 8 9 10
1 i = 1 2 while i < 11: 3 if i!= 7: 4 print(i) 5 else: 6 pass 7 i+=1
# 2、求1-100的所有数的和
i = 1 sum = 0 for i in range(1,101): i = i+sum sum = 0+i print(sum)
# 3、输出 1-100 内的所有奇数
for i in range(1,101): if i%2 == 1: print(i) i = i+1
# 4、输出 1-100 内的所有偶数
i = 1 while i < 101: if i%2 == 0: print(i) i +=1
# 5、求1-2+3-4+5 ... 99的所有数的和
sum_i = 1 for i in range(1,101): if i%2 == 1: #print(i) sum_i = sum_i + i i = i+1 #print(sum_i) j = 1 sum_j = 2 while j < 101: if j%2 == 0: #print(j) sum_j = sum_j +j j = j+1 sum = sum_i - sum_j print("1-2+3-4+5 ... 99的所有数的和是{}".format(sum)) print("1-2+3-4+5 ... 99的所有数的和是%s"%(str(sum))) print("1-2+3-4+5 ... 99的所有数的和是{0}".format(sum)) print("1-2+3-4+5 ... 99的所有数的和是{sum}".format(sum = sum))
# 6、用户登陆(三次机会重试)
count = 0 sum = 2 username = "XF" password = "123456" while count < 3: usr = str(input("请输入账号:")).strip() psd = str(input("请输入密码:")).strip() if usr == username and psd == password: print("登录成功") break elif usr == username and psd != password: sum = sum - count print("密码错误,还有%s次输入机会"%sum) elif usr != username and psd == password: sum = sum - count print("无此账号,还有%s次输入机会"%sum) elif usr != username and psd != password: sum = sum - count print("账号名或密码错误,还有%s次输入机会"%sum) count = count+1