1. 程式人生 > >語言基礎day1 練習題

語言基礎day1 練習題

#使用while迴圈輸出1 2 3 4 5 6 8 9 10 (方法1)

1 count = 1
2 while count < 11:
3     print(count)
4     count = count + 1
5     if count == 7:
6         count = count + 1
7 print("----end----")

#使用while迴圈輸出1 2 3 4 5 6 8 9 10 (方法2)

1 count = 1
2 while count < 11:
3     if count == 7:
4         pass
5 else: 6 print(count) 7 count = count + 1 8 9 print("----end----")

#輸出1-100 內的所有奇數

1 count = 1
2 while count < 101:
3     if count % 2 != 0:
4         print(count)
5     count = count +1

#輸出1-100 內的所有偶數

1 count = 1
2 while count < 101:
3     if count % 2 == 0:
4         print
(count) 5 count = count +1

#求1-100的所有數的和

1 count = 1
2 a = 0
3 while count < 101:
4     a = a + count
5     count = count +1
6 print(a)

#求1-2+3-4+5 ... 99的所有數的和(方法1)

 1 count = 1
 2 a = 0
 3 b = 0
 4 while count < 100:
 5     if count % 2 != 0:
 6         a = count + a
 7         count = count + 1
 8
else: 9 b = b -count 10 count = count + 1 11 print(a+b)

#求1-2+3-4+5 ... 99的所有數的和(方法2)

 1 count = 1
 2 a = 0
 3 while count < 100:
 4     temp = count % 2
 5     if temp == 0:
 6         a = a - count
 7     else:
 8         a = a + count
 9     count = count + 1
10 
11 print(a)

 

#使用者登入(三次機會)

 1 user_id = "zxf"
 2 pass_word = "123"
 3 count = 3
 4 name = input("請輸入賬號:")
 5 mima = input("請輸入密碼:")
 6 while count > 1:
 7     count = count -1
 8     if name == user_id and mima == pass_word:
 9         print("歡迎登入")
10         count = 0
11     elif name != user_id or mima != pass_word and count > 2:
12         print("賬號或密碼不正確請重試") 
13         name = input("請輸入賬號:")
14         mima = input("請輸入密碼:")
15     if count == 1:
16         print("錯誤多次無法登入。")