while循環

示例:input

age_of_oldboy = 56it

count = 0 while True:循環

    if count == 3:im

        #只能嘗試三次db

        breakdi

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

    if guess_age == age_of_oldboy :while

        print("yes,you got it.")co

        break         # 猜對就跳出循環time

    elif guess_age > age_of_oldboy:

        print("think smaller...")     #給第二種可能,不是就有可能

    else:

        print("think bigger!")

    count += 1

 

示例2:

age_of_oldboy = 56
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 keep guessing?")#這4行是能夠詢問是否繼續
        if countine_confirm != 'n':
            count = 0
else:
    print("you have tried too many times..")
相關文章
相關標籤/搜索