Python練習代碼
阿新 • • 發佈:2018-10-09
成功 輸出 判斷 pre 輸入 賬號 pass num python
1-100所有數字的和
count = 1 sum = 0 while count <= 100: sum = sum + count count += 1 print(sum)
輸出1-100內的所有奇數
count = 1 num = 0 while count <= 100: count += 1 num += 1 if num%2 == 0: continue else: print(num)
輸出1-100內所有的偶數
count = 1 num = 0 while count <= 100: count += 1 num+= 1 if num%2 == 0: print(num)
求1-2+3-4+5...99的和
count = 1 num = 1 sum = 0 while count <= 99: if num%2 == 0: num = -num #判斷奇數偶數,奇數為正,偶數為負 else: num = num sum = sum + num count += 1 num += 1 print(sum)
使用while循環輸入1 2 3 4 5 6 8 9 10
count = 1 num= 0 while count < 10: count += 1 num += 1 if num == 7:continue else:print(input(‘請輸入數字‘+str(num)))
用戶登錄(三次機會重試)
count = 1 while count <= 3: ID = input(‘請輸入賬號:‘) Password = input(‘請輸入密碼:‘) count += 1 if ID == ‘關元最帥‘ and Password == ‘無敵帥‘: print(‘登陸成功‘)break elif count <= 3: print(‘登陸失敗,還有‘ + str(4-count) + ‘次機會‘) else: print(‘登陸失敗超過三次‘)
Python練習代碼