循环就是这么简单

1,while

检测输入的值 是否合适的,while简单应用。

_age = 30
count = 0
total = 3
while count < total:
    age = int(input("guess age : "))
    if _age < age:
        print("it's larger")
    elif _age == age:
        print("you got it")
        break
    else:
        print("it's smaller")

    count += 1
    if total - count > 0:
        print("you  already left %d times" % (total - count))
else:
    print("you have already used all your chance, Bye"

or 不带else的常规用法

_age = 30
count = 0
total = 3
while count < total:
    age = int(input("guess age : "))
    if _age < age:
        print("it's larger")
    elif _age == age:
        print("you got it")
        break
    else:
        print("it's smaller")

    count += 1
    if total - count > 0:
        print("you  already left %d times" % (total - count))

2,for
for循环的用法

for i in range(0,10,2):
    print(i)

_age = 2

count = 0
total = 3

for i in  range(3):
    age = int(input("guess age : "))
    if _age < age:
        print("it's larger")
    elif _age == age:
        print("you got it")
        break
    else:
        print("it's smaller")

    if total - i-1 > 0:
        print("you  already left %d times" % (total - count))
else:
    print("you have already used all your chance, Bye")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容