for i in range(10):
print("loop",i)

i是临时变量,其实是for i in(0,1,2,3,4,5,6,7,8,9)

for-else

age_of_oldboy = 56
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 smaller...")
else:
print("think bigger!")
else:
print("you have tried too many times...!")

for下面的正常走完,才走else,不是正常走完,不走else

for i in range(0,10,2):
print("loop",i)

2是每隔一个跳一个,相当于步长,默认1

age_of_oldboy = 56
count = 0
while count < 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 smaller...")
else:
print("think bigger!")
count += 1
if count == 3:
countine_confirm = input("do you want to keep guessing...?")
if countine_confirm != 'n':
count = 0
else:
print("you have tried too many times...!")
12-18 13:36