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