1. 程式人生 > >python while基礎實例

python while基礎實例

輸入 tro pri strong IT HA elif 基礎 put

1、循環十次退出while循環:

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 #跳出循環就不走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、達到3次,提示是否繼續競猜,輸入n退出:
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

python while基礎實例