猜数字程序:
# @Author Jonathon
# @Software: PyCharm Community Edition
# @time:2017/10/2319:59
age=56
count=0
while count<3:
age111 =int(input('age:'))
if age111==age:
print('you are right')
break
elif age111>age:
print('please smaller')
else:
print('please bigger')
count+=1 #如果三个if else后面都加break,count这一句就会报错
if count==3:
continue_flag=input('do you want go on?')
if continue_flag!='n':
count=0
else:
print('too many times.please wait one hour')
模拟登陆(与猜数字类似)
# @Author Jonathon
# @Software: PyCharm Community Edition
# @time:2017/10/2321:10
import os
import json
count=0
records=[]
locks=[]
login_flag=False
path='D:/namepwd.txt' #文本文件中的字典key,value必须是双引号,否则pycharm报错
for line in open(path):
records.append(json.loads(line))
while count<3:
username=input('请输入用户名:')
password=input('请输入密码:')
for item in records:
if (item['name']==username and item['pwd']==password):
print('welcome {name}'.format(name=username))
login_flag=True
break
else:
continue
if login_flag==True:
break
count += 1
if count==3:
print('超过三次错误,已经锁定!') #如果三次都没输入正确,锁定帐号
with open('D:/locks.txt','a') as file: #追加写入文件
file.write('\n')
file.write(username)
break