python3 中默認採用utf-8編碼,不須要添加python
# -*- coding:utf-8 -*-
python2 中默認ascii編碼,須要添加.函數
import example 時,會把example.py 轉成字節碼文件 example.pyc 再使用example.pyc 。因此能夠不存在 .py 文件,只存在 .pyc 文件。編碼
import getpass pwd = getpass.getpass('Password:') #使輸入密碼不可見
python3 中輸入函數用 input() ,若使用 raw_input() 會報錯 NameError: name 'raw_input' is not definedspa
python2 中輸入函數用raw_input() (有時,如2.7.11也能夠用input() )code
做業:blog
#!/usr/bin/env python # -*- coding:utf-8 -*- #輸出 1-100 內的全部奇數 n = 1 while True: if n % 2 == 1: print(n) if n == 100: break n += 1 print('End')
#!/usr/bin/env python # -*- coding:utf-8 -*- #輸出 1-100 內的全部偶數 n = 1 while True: if n % 2 == 0: print(n) if n == 100: break n += 1 print('End')
#!/usr/bin/env python # -*- coding:utf-8 -*- #上一題略加改進 n = 1 while n < 101: if n % 2 == 1: print(n) #pass else: pass #表示什麼也不執行,固然能夠不加else #print(n) #二者對調能夠輸出偶數,本身咋就沒想到呢 n += 1 print('END')
#!/usr/bin/env python # -*- coding:utf-8 -*- #求1-2+3-4+5 ... 99的全部數的和 n = 1 sum = 0 while True: if n % 2 == 1: sum += n elif n % 2 == 0: sum -= n if n == 100: print(sum) break n += 1 print('End')
#!/usr/bin/env python # -*- coding:utf-8 -*- #改進上一題 s = 0 n = 1 while n < 100: tem = n % 2 if tem == 1: s += n else: s -= n n += 1 print(s)
#!/usr/bin/env python # -*- coding:utf-8 -*- #用戶登錄(三次機會重試) import getpass n = 1 while True: user = raw_input('Input Your name:') pwd = getpass.getpass('Input Your Password:') if user == 'root' and pwd == 'toor': print('Success') break else: print('Try Again') if n == 3: print('3 Times Used') break n += 1 print('END')
#!/usr/bin/env python # -*- coding:utf-8 -*- #改進上一題 i = 0 while i < 3: user = raw_input("Username: ") pwd = raw_input("Password: ") if user == "h" and pwd == "1": print("Success!") break else: print("Tyr Again!") i += 1
--------------------------------------------------------------utf-8