python while basic example

1. Loop ten times to exit the while loop:

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

2、猜年纪:
age_of_oldboy = 56 

#guess_age = int(input("guess age:"))

count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("yes, you got it. ")
break #Don't go out of the loop else
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count+=1
else:
print("you have tried too many times.. ")

3. When it reaches 3 times, it will prompt whether to continue the guessing. Enter n to exit:
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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782303&siteId=291194637