1 字符串格式化
#!/urs/bin/env python name = input("Name: ")
age = int(input("Age: "))
job = input("Job: ")
salary = input("Salary: ") info1 = """ ----------- info1 of %s ---------
name: %s
age: %s
job: %s
Salary: %s
""" % (name, name, age, job, salary) print(info1) info2 = """ ----------- info2 of {_name} ----
Name: {_name}
Age: {_age}
Job: {_job}
Salary: {_salary}
""".format(_name=name,
_age=age,
_job=job,
_salary=salary) print(info2) info3 = """ ------ info3 of {0} -------
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
""".format(name, age, job, salary) print(info3)
2 密码非明文显示
#!/urs/bin/env python
# -*- coding:utf-8 -*- import getpass
username = input("username:")
password = getpass.getpass("password:") print(username)
print(password)
注在Pycharm中不可以,在命令行模式运行:python xxx.py可以
模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
#!/urs/bin/env python
# -*- coding:utf-8 -*- lock_users = []
f0 = open("lock_users.db", encoding="utf-8")
for line in f0.readlines():
line = line.strip()
lock_users.append(line)
f0.close() users_list = []
f1 = open("users.db", "r", encoding="utf-8")
for line in f1.readlines():
line = line.strip().split()
users_list.append(line[0])
users_list.append(line[1])
f1.close() times = 3 for i in range(times):
username = input("Enter your name:")
password = input("Enter your password:") if username in lock_users:
print("\tSorry, you're locked now. Please contact administrators.")
break if username in users_list and password == users_list[users_list.index(username) + 1]:
print("\tWelcome %s login !" % username)
break
else:
print("\tusername or password is wrong.") if i == times-1:
f2 = open("lock_users.db", "a+", encoding="utf-8")
f2.write("\n")
f2.write(username)
f2.close()
print("\t{}, you're locked!".format(username))
三级菜单:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
4. 让用户选择是否要退出
5. 有返回上一级菜单的功能
评分标准:
用多层嵌套while循环的方式完成作业2,85分
只用一层循环完成作业2,100分
#!/urs/bin/env python
# -*- coding:utf-8 -*- data ={
'北京':{
'海淀区':{
'颐和园', '圆明园遗址公园', '旧土城遗址公园', '大钟寺'
},
'东城区':{
'劳动人民文化宫', '老舍纪念馆', '柳荫公园', '青年湖公园'
},
},
'浙江':{
'杭州':{
'西湖', '西溪湿地', '宋城', '钱塘江', '南宋御街',
},
'绍兴':{
'柯岩风景区、大禹陵、东湖、鲁迅故里',
},
},
'山东':{
'济南':{
'趵突泉 大明湖 千佛山 芙蓉街(小吃街)',
},
'青岛':{
'栈桥,小鱼山,海底世界,第一海水浴场,八大关风景区, 花石楼',
},
},
} flag = True while flag:
for i in data:
print(i)
choose1 = input(">") if choose1 == "q":
flag = False while flag:
for j in data[choose1]:
print("\t", j) choose2 = input(">>")
if choose2 == 'b':
break
elif choose2 == 'q':
flag = False while flag:
for k in data[choose1][choose2]:
print("\t\t",k) choose3 = input(">>>")
if choose3 == 'b':
break
elif choose3 == 'q':
flag = False
#!/urs/bin/env python
# -*- coding:utf-8 -*- menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
} exit_flag = False
current_layer = menu layers = [menu] while not exit_flag:
for k in current_layer:
print(k)
choice = input(">>:").strip()
if choice == "b":
current_layer = layers[-1]
#print("change to laster", current_layer)
layers.pop()
elif choice not in current_layer:continue
else:
layers.append(current_layer)
current_layer = current_layer[choice]
最多可连续3次猜年龄
#!/urs/bin/env python
# -*- coding:utf-8 -*- """
最多可连续3次猜年龄
"""
"""
_age = 56
count = 3 while count > 0:
age = int(input("age = "))
if age == _age:
print("You're right.")
break
elif age > _age:
print("think smaller ... ")
else:
print("think bigger ... ")
count -= 1
else:
print("Sorry, there's not times for you.")
""" _age = 56 for i in range(3):
age = int(input("age = "))
if age == _age:
print("You're right.")
break
elif age > _age:
print("think smaller ... ")
else:
print("think bigger ... ")
else:
print("Sorry, there's not times for you.")
可参看网址:
http://www.cnblogs.com/alex3714/articles/5717620.html