1. 程式人生 > >Python 之 while循環語句

Python 之 while循環語句

while inpu ESS 最簡 else語句 while循環 pre print 循環

1、最簡單的while循環語句
count = 0
while True:
    count = count +1
    print(count)

2、加上條件判斷循環

age = 23
count = 0
while count < 3:
    age_guess = int(input("請輸入數字:"))
    if age_guess == age :
        print("你猜對了啦")
        break;
    elif age_guess > age :
        print("你猜大了")
    else:
        print("你猜小了")
    count = count+1

print("遊戲結束")

3、while循環另類用法(else)

age = 23
count = 0
while count < 3:
    age_guess = int(input("請輸入數字:"))
    if age_guess == age :
        print("你猜對了啦")
        break;
    elif age_guess > age :
        print("你猜大了")
    else:
        print("你猜小了")
    count = count+1
else:                   #當while判斷不成立時,運行else語句
    print("你輸入的次數太多啦")     

print("遊戲結束")

Python 之 while循環語句