python while基础实例

Posted 无所住心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python while基础实例相关的知识,希望对你有一定的参考价值。

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基础实例的主要内容,如果未能解决你的问题,请参考以下文章

Python基础之循环

Python3基础学习-while循环实例- 猜数字游戏

python 循环基础

Python基础---循环--条件判断(while-for-if-elif-else)

python 基础 5 while循环语句

Python3基础12——while循环