1. 程式人生 > >Python入門之練習題

Python入門之練習題

嵌套 輸入 body 猜數字 als clas span print 循環嵌套

  • 寫循環猜年齡程序,猜錯三次則打印提示信息並退出循環,猜對也打印提示信息並退出循環
count=0
while count < 3:
    num = input("猜年齡遊戲:")
    guess = int(num)
    if guess == 18:
        print("恭喜你猜對了")
    elif guess > 18:
        print("大了")
    elif guess < 18:
        print("小了")
    count += 1
else:
    print("猜了三次都沒猜對‘
")
  • 寫while循環嵌套的小例子,用一個tag控制所有while循環的退出
count=0
tag="True"
while True:
    if count >= 3:
        tag="False"
        break
    username = input("請輸入用戶名:")
    password = input("請輸入密碼:")
    if username == "hello" and password == "123":
        print("登錄成功")
        while True:
            
if count >= 3: print("猜了三次也沒猜中!") break print("猜數字:") guess=int(input("請輸入一個數字:")) if guess == 18: print("猜對了") elif guess > 18: print("大了") else: print(
"小了") count += 1 else: print("用戶名不存在或密碼錯誤!") count +=1

Python入門之練習題