老男孩教学学习笔记:
实例1:格式化输出
# Author:Alex Liname = input("name:")# raw_input 2.x input 3.x# input 2.x =age = int(input("age:")) # integerprint(type(age), type(str(age) ))job = input("job:")salary= input("salary:")info = '''-------- info of %s -----Name:%sAge:%dJob:%sSalary:%s''' % (name, name, age, job, salary)info2 = '''-------- info of {_name} -----Name:{_name}Age:{_age}Job:{_job}Salary:{_salary}'''.format(_name=name, _age=age, _job=job, _salary=salary)info3 = '''-------- info of {0} -----Name:{0}Age:{1}Job:{2}Salary:{3}'''.format(name, age, job, salary)print(info)print(info2)print(info3)
实例2:密码密文输入
# Author:Alex Liimport getpass_username = 'alex'_password = 'abc123'username = input("username:")# password = getpass.getpass("password:")password = input("password:")if _username == username and _password == password: print("Welcome user {name} login...".format(name=username))else: print("Invalid username or password!")
实例3:
猜年龄(一次)
# Author:Alex Li age_of_oldboy = 56guess_age = int(input("guess age:"))if guess_age == age_of_oldboy: print("yes, you got it")elif guess_age > age_of_oldboy: print("think smaller ...")else: print("think bigger!")
猜年龄(多次)——循环
# Author:Alex Liage_of_oldboy = 56count = 0while count < 3: guess_age = int(input("guess age:") ) if guess_age == age_of_oldboy: print("yes, you got it. ") break elif guess_age > age_of_oldboy: print("think smaller...") else: print("think bigger!") count += 1else: print("you have tried too many times..bye")
循环学习:
# Author:Alex Li'''count = 0while True: print("count:",count) count = count +1 #count +=1 if count == 1000: break''''''for i in range(0,10): if i <3: print("loop ",i) else : continue print("hehe...")'''for i in range(10): print('----------',i) for j in range(10): print(j) if j > 5: break
循环结束后询问是否继续:
# Author:Alex Liage_of_oldboy = 56count = 0while count < 3: guess_age = int(input("guess age:") ) if guess_age == age_of_oldboy : print("yes, you got it. ") break elif guess_age > age_of_oldboy: print("think smaller...") else: print("think bigger!") count += 1 if count == 3: countine_confirm = input("do you want to keep guessing..?") if countine_confirm != 'n': count = 0
作业:
一、编写登录接口:
1、输入用户名密码
2、认证成功后显示欢迎信息
3、用户名3次输入错误后,退出程序
4、密码3次输入错误后,锁定用户名
二、多级菜单
三级菜单
可依次选择进入各子菜单
所需知识点:列表、字典