_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")
for i in range(3): print(i)
输出结果
0
1
2
以下代码只能在python2.x才能看出来
for i in range(3): print("loop:",i)
输出结果
loop: 0
loop: 1
loop: 2
规律:前包后不包
打印1-100的所有奇数
for i in range(1,101): if i%2 == 1: print("loop:",i)
方法二:
for i in range(1,101,2): print("loop:",i)
打印1-100的数字,50-70不打印
for i in range(1,101): if i>=50 and i<=70 : continue; print(i)
方法二:
for i in range(1,101): if i<50 or i>70 : print(i)
最多输入3次的案例
for i in range(3): username = input("Username:") password = input("Password:") if username == _user and password == _passwd: print("Welcome %s login..." %_user) break else: print("Invalid username or password")
时间: 2024-11-06 20:15:47