Python 知識學習

1. while…else…

_user = 'cxc'
_passwd = 'abc123'

counter = 0
while counter < 3:
    username = input('username:')
    password = input('password:')
    if username == _user and password == _passwd:
        print("welcome %s login......" % _user)
        break
    else:
        print("invalid username or password")
    counter += 1
    if counter == 3:
        keep_going_choice = input('還想玩嗎?[y/n]')
        if keep_going_choice == 'y':
            counter = 0
else:
    print('此賬號已被封鎖')

此時定義了循環三次,只要循環走滿三次,會直接走最後的else語句,

2. enumerate()

 list = range(5)
 for i in enumerate(list):
 	print(i)
 	
///
    (0, 0)
	(1, 1)
	(2, 2)
	(3, 3)
	(4, 4)

此方法還可以添加參數,指定前面顯示的數字是從多少開始的,另外這個數字不是下標,是按照排列的順序遍歷的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章