#把手機號中間四位隱藏 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()