Ericbig 2020-02-25
students = ['egon', 'lxx', 'alex'] i = 0 while i < 3: print(students[i]) i += 1
students = ['egon', 'lxx', 'alex'] for x in students: # 有几个值就循环几次 print(x) dic={'name':'egon','age':18,'sex':'male'} for aaa in dic: # 从字典里取出的默认是key, print(aaa,dic[aaa]) range(1,3) # 从1开始,顾头不顾尾 for x in range(1,3): # [1,2] print(x)
range(3):默认从0开始,顾头不顾尾,到2结束,0,1,2 range(1,6) # 可以指定开头,从1开始,顾头不顾尾,到5结束,1 2 3 4 5 range(1,6,2) # 可以指定步长,1 3 5
相同之处:都是循环,都是用来做重复的事情
不同之处:
for i in range(3): username=input('username>>: ') password=input('password>>: ') if username == 'gxy' and password == '123': break else: print('用户名或密码错误')
for i in range(5): # [0,1,2,3,4] if i == 2 or i == 3:continue print(i)
for i in range(3): # username=input('username>>: ') password=input('password>>: ') if username == 'gxy' and password == '123': break else: print('用户名或密码错误') else: print('账号密码输错次数达到最大限制,退出程序')