循環(huán)就是這么簡(jiǎn)單

1,while

檢測(cè)輸入的值 是否合適的,while簡(jiǎn)單應(yīng)用。

_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的常規(guī)用法

_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循環(huán)的用法

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")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容