1、用户登陆
i=0 while i<3: i += 1 j = 3 - i username = input("请输入姓名:") password = input("请输入密码:") if username == "小二" and password == "123": print("登陆成功") else: if username != "小二": print("帐户名错误") print("你还有%d次机会" % (j)) if i == 3: answer = input("再试试?") if answer == "y": i = 0 elif username == "小二": if password != "123": print("密码错误") print("你还有%d次机会" % (j)) if i == 3: answer = input("再试试?") if answer == "y": i = 0
2、格式化输出
1 name = input('请输入姓名') 2 age = input('年龄') 3 4 msg = "我是%s,今年%s岁" %(name,age) 5 6 print(msg)
3、不包括88,求1-2+3-4...+99-100的和
1 i=0 2 sum = 0 3 while i<100 : 4 i += 1 5 if i == 88 : 6 continue 7 elif i % 2 == 0: 8 sum -= i 9 else: 10 sum += i 11 print(sum)
1 i = 0 2 sum = 0 3 while i<100 : 4 i += 1 5 if i == 88 : 6 continue 7 elif i<88 : 8 if i % 2 == 0: 9 sum -= i 10 else: 11 sum += i 12 else: 13 if i % 2 == 0: 14 sum += i 15 else: 16 sum -= i 17 print(sum) 18 print(i)