python学习第一周之while循环

1.while循环语句

hile True:
    print("count:",count)
    count = count +1 #count +=1
    if count == 10:
        break

2.(1)while循环的使用,以下为最多输入三次数,退出。

age_of_oldboy =  56

count = 0
while True:
 if count == 3:
     break
 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#计数一般在循环最后边

2(2)对于(1)的优化,当count小于3时执行循环


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#计数一般在循环最后边

2(3)while与else的用法,当while的条件不成立时,执行else语句

age_of_oldboy =  56

count = 0
while count<5:
 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:        #当count<3不成立时,执行else语句
 print("try too many")

 

你可能感兴趣的:(python学习第一周之while循环)