1.0 输入用户名,密码练习
_user = "alex" _passwd = "abc123" username = input("Username:") password = input("Password:") if username == _user and password == _passwd: print("Welcome %s login..." %_user) else: print("Invalid username or password") #>>>Username:alex #>>>Password:abc123 #>>>Welcome alex login...
2.0 循环0-3(不包括3的数字)
for i in range(3): print(i) #>>>0 #>>>1 #>>>2
3.0 以下命令只能在python2.x模式下才能显示
>>>range(3) [0,1,2]
4.0 for案例
for i in range(3): print("loop:",i) #>>>loop:0 #>>>loop:1 #>>>loop:2
3.1 range测试,只适用于python2.x版本中(规律:前包后不包)
>>> range(1,3) [1,2] >>>range(1,4) [1,2,3] >>>range(2,5) [2,3,4]
5.0 打印1-100的所有奇数
for i in range(1,101): if i%2 == 1: print("loop:",i)
5.1 方法二:
for i in range(1,101,2): print("loop:",i)
5.2 打印1-100的数字,50-70不打印
for i in range(1,101): if i>=50 and i<=70 : continue; print(i)
5.2.2 方法二
for i in range(1,101): if i<50 or i>70 : print(i)
5.3 最多输入3次的案例
username_y = 'alex' password_y = 'abc123' for i in range(3): username = input("Username:") password = input("Password:") if username == username_y and password == password_y: print("Welcome %s login..." %username) break else: print("Invalid username or password")