1. 程式人生 > >python基礎迴圈

python基礎迴圈

1、使用while迴圈輸入1234568910
 n = 1
 while n < 11:
     if n == 7:
         pass
     else:
         print(n)
     n = n + 1
 
2、求1 - 100的所有數的和
 
 
 
n = 1
s = 0
while n < 101:
    s = s + n
    n = n + 1

print(s)
 
3、輸出1 - 100內的所有奇數

n = 1
while n < 101:
    temp = n % 2
    if temp == 0:
        
pass else: print(n) n = n + 1 print('----end----')
 
4、輸出1 - 100內的所有偶數

n = 1
while n < 101:
    temp = n % 2
    if temp == 0:
        print(n)
    else:
        pass
    n = n + 1

print('----end----')

  5、求1 - 2 + 3 - 4 + 5...99的所有數的和

n = 1
s = 0  # s是之前所有數的總和
while
n < 100: temp = n % 2 if temp == 0: s = s - n else: s = s + n n = n + 1 print(s)
6、設計登入小程式,有三次機會

import datetime
username = "keys"
password = "123456"
no = 2
while no >= 0:
    input_name = input("請輸入你的賬號:")
    input_pswd = input("請輸入你的密碼:")
    if username == input_name and
password == input_pswd: print("恭喜您"+username+"登入成功,今天是{}".format(datetime.datetime.today())) break elif input_name == "" or input_pswd == "": print("使用者名稱或密碼不能為空,請重新輸入") print("您還有"+str(no)+"次機會!!!") else: if username != input_pswd or password != input_pswd: print("\n賬號或密碼錯誤,請重新輸入") print("溫馨提示,您還有"+str(no)+"次機會") else: print("您的次數已用完。") no = no -1