while 或 for 可以与 else连用,如未完成条件的状态下退出,则会执行else中语句。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 while count < 3: 2 guess_age = int(input("guess age: ")) 3 if guess_age == age_of_oldboy: 4 print("yes, you got it. ") 5 break 6 elif guess_age > age_of_oldboy: 7 print("think smaller...") 8 else: 9 print("think bigger!") 10 count += 1 11 else: 12 print("you have tried too many times..fuck off")
几种字符串打印格式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 username = input("username: ") 2 password = input("password: ") 3 age = input("age: ") 4 job = input("job: ") 5 salary = input("salary: ") 6 7 info = ''' 8 --------- info of %s --------- 9 Name: %s 10 Age: %s 11 Job: %s 12 Salary: %s 13 ''' % (username, age, password, job, salary) 14 15 print(info) 16 17 info2 = ''' 18 --------- info of {_name} --------- 19 Name: {_name} 20 Age: {_age} 21 Job: {_job} 22 Salary: {_salary} 23 '''.format(_name=username, 24 _age=age, 25 _job=job, 26 _salary=salary) 27 28 print(info2) 29 30 info3 = ''' 31 --------- info of {0} --------- 32 Name: {0} 33 Age: {1} 34 Job: {2} 35 Salary: {3} 36 '''.format(username,age,job,salary) 37 print(info3)
隐藏输入密码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
password = getpass.getpass("password: ")
作业一: 编写登录接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 错三次后锁定
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python3.6 2 # Author: Binglin Ji 3 import getpass 4 import csv 5 with open('user_info.csv', 'r') as f: 6 reader = csv.reader(f) 7 user_info = list(reader) 8 with open('bid_info.csv', 'r') as f: 9 reader = csv.reader(f) 10 bid_user = list(reader) 11 12 locked = False 13 for i in range(3): 14 username = input("username: ") 15 password = getpass.getpass("password: ") 16 for name in bid_user: 17 if name[0] == username: 18 print("Your account has been locked!") 19 locked = True 20 break 21 22 for user in user_info: 23 if user[0] == username and user[1] == password and locked == False: 24 print("welcome user {name} login...".format(name=username)) 25 break 26 else: 27 print("authorization failed, please try again") 28 if i == 2: 29 f = open('bid_info.csv','w') 30 f.write(username) 31 f.close() 32 print("your account has been locked, please contact with the administrator.")