If...Else
1 _username = "A1_Code" 2 _password = "123" 3 4 username = input("username:") 5 password = input("password:") 6 7 if _username == username and _password == password: 8 print("Welcome user {name} login...".format(name=username)) 9 else:10 print("Invalid username or password!")
If...Elif...Else
1 age_of_oldboy = 562 guess_age = int(input("guess age:"))3 4 if guess_age == age_of_oldboy:5 print("yes,you got it.")6 elif guess_age > age_of_oldboy:7 print("think smaller...")8 else:9 print("think bigger!")
while
1 count = 02 while True:3 print("count:", count)4 count = count + 1 #count+=1
Break 跳出循环
1 count = 02 while True:3 print("count:", count)4 count = count + 1 #count+=15 if count == 1000:6 break
加计数器
1 age_of_oldboy = 56 2 3 count = 0 4 while True: 5 if count == 3: 6 break 7 guess_age = int(input("guess age:")) 8 if guess_age == age_of_oldboy: 9 print("yes,you got it.")10 break11 elif guess_age > age_of_oldboy:12 print("think smaller...")13 else:14 print("think bigger!")15 16 count += 1
While 后加条件
1 age_of_oldboy = 56 2 3 count = 0 4 while count < 3: 5 if count == 3: 6 break 7 guess_age = int(input("guess age:")) 8 if guess_age == age_of_oldboy: 9 print("yes,you got it.")10 break11 elif guess_age > age_of_oldboy:12 print("think smaller...")13 else:14 print("think bigger!")15 16 count += 1
While...Else
1 age_of_oldboy = 56 2 3 count = 0 4 while count < 3: 5 if count == 3: 6 break 7 guess_age = int(input("guess age:")) 8 if guess_age == age_of_oldboy: 9 print("yes,you got it.")10 break11 elif guess_age > age_of_oldboy:12 print("think smaller...")13 else:14 print("think bigger!")15 count += 116 else:17 print("you have tried too many times...fuck off")
For
1 for i in range(10):2 print("loop", i)
For...Else
1 age_of_oldboy = 56 2 3 for i in range(3): 4 guess_age = int(input("guess age:")) 5 if guess_age == age_of_oldboy: 6 print("yes,you got it.") 7 break 8 elif guess_age > age_of_oldboy: 9 print("think smaller...")10 else:11 print("think bigger!")12 else:13 print("you have tried too many times...fuck off")
For 跳跃循环
1 for i in range(0, 10, 2):2 print("loop", i)
优化
1 age_of_oldboy = 56 2 3 count = 0 4 while count < 3: 5 if count == 3: 6 break 7 guess_age = int(input("guess age:")) 8 if guess_age == age_of_oldboy: 9 print("yes,you got it.")10 break11 elif guess_age > age_of_oldboy:12 print("think smaller...")13 else:14 print("think bigger!")15 count += 116 if count == 3:17 countine_confirm = input("Do you want to keep guessing..?")18 if countine_confirm != 'n':19 count = 0
加断点 Debug
Continue 跳出本次循环,继续下次循环;Break 结束循环
1 for i in range(0, 10):2 if i < 3:3 print("loop", i)4 else:5 continue6 print("hehe...")
循环套循环
1 for i in range(10):2 print('----------', i)3 for j in range(10):4 print(j)5 if j > 5:6 break