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
优化后的代码:
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
else:
print("you have tried too mant times...fuck off")
for循环的代码:
age_of_oldboy = 56
count = 0
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 mant times...fuck off")
continue: 跳出本次循环,继续到下一次循环
break:结束整个循环
来源:https://www.cnblogs.com/Cohen/p/7348853.html