循环
- while break:
count = 0 while True: count +=1#count = count + 1 print(count) if count == 100: break
- while 条件
while count < 3 : #注意这里需要: guest_age = int(input("guess age:")) if guest_age == age_of_oldboy : print("yes! you got it") break #跳出循环 elif guest_age > age_of_oldboy : print("you guess too bigger") else: print("you guess too small") count +=1 #count = count + 1
- while else
格式:
while 条件:
print()
else:
print()
while count < 3 : #注意这里需要: guest_age = int(input("guess age:")) if guest_age == age_of_oldboy : print("yes! you got it") break #跳出循环 elif guest_age > age_of_oldboy : print("you guess too bigger") else: print("you guess too small") count +=1 #count = count + 1 else: print("you guess too much !Fuck !")
- for循环
for i in range(10):#相当于for i in range(0,10.1),意思是从0开始,数10个,间隔为1 print(i) for i in range(0,10,2):#10为i从0开始数10次,2为间隔多少打印,这里为间隔2 print(i)
- for else
for i in range(3): guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("yes! you got it") break elif guess_age > age_of_oldboy: print("think small..") else: print("think bigger") else: print("fock you")
- break 跳出当前循环,结束这个循环
for i in range(10): print("----------------",i) for j in range(10): if j >4: break print(j)
- continue:跳出本次继续,继续下一次循环
for i in range(10): if i<3: print("loop:",i) else: continue print("hahahha")
- 好