python--小确幸
#把手机号中间四位隐藏 def change_number(number): hiding_number=number.replace(number[3:7],'*'*4) print(hiding_number) number=input('请输入电话号码:') hid_number=change_number(number)
#输入密码,判断是否正确 def count_login(): password=input('password:') if password=='12345': print('succeed!!') else: print('password wrong!') count_login() count_login()
#输入密码,判断是否正确,输错三次就在等一小时 global num#全局变量要先声明再使用 num=0 def count_login(): global num#全局变量在函数中用到时要再次声明 password=input('password:') if password==('12345'): print('succeed!!') else: print('password wrong!') num=num+1 if num==3: print('Please try again 1 hour later!') else: count_login() count_login()
#用循环实现:输入密码判断是否正确,若错误3次就再等1小时 i=0 while i<3: password=input('passward:') if password==('12345'): print('Succeed!!') exit(0) else: i=i+1 print('Password wrong!') print('Please try again 1 hour later!')
#文件打开和编辑文件内容 f = open('C:/Users/Ma Yiling/Desktop/code/Cola.txt', 'w') #清空文件内容再写 f.write('aaa') #只能写字符串 f.write('\n') f.writelines(['123', 'bbb']) #可写所有能迭代的类型,例如list f.writelines(('456','\n', 'ccc')) #例如tuple f.close()