1.使用while 循環輸入 1 2 3 4 5 6 7 8 9 10
ide
num = 1
while num < 11:
print(num)
num += 1
2.求1-100的全部數的和num = 1sum = 0while num <101: sum += num num += 1print(sum)3.輸出1-100內的全部奇數num = 1while num < 101: temp = num % 2 if temp == 0: print(num) num += 1 4.輸出1-100內的全部偶數sum = 1while sum < 100: tang = sum % 2 if tang != 0: print(sum) sum += 1 5.模擬用戶登陸,容許用戶錯三次(帳號爲tang,密碼123456)sum = 0while sum < 3: user = input("請輸入帳號:") password = input("請輸入密碼:") if user == "tang": if password == "123456": print("登陸成功") break else: print("密碼錯誤") else: print("沒有此用戶") sum += 1