• Python 学习笔记【05】流程控制


      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 = 56
    2 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 = 0
    2 while True:
    3     print("count:", count)
    4     count = count + 1 #count+=1

      Break 跳出循环

    1 count = 0
    2 while True:
    3     print("count:", count)
    4     count = count + 1 #count+=1
    5     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         break
    11     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         break
    11     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         break
    11     elif guess_age > age_of_oldboy:
    12         print("think smaller...")
    13     else:
    14         print("think bigger!")
    15     count += 1
    16 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         break
    11     elif guess_age > age_of_oldboy:
    12         print("think smaller...")
    13     else:
    14         print("think bigger!")
    15     count += 1
    16     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         continue
    6     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
  • 相关阅读:
    poj 3744 题解
    hdu 1850 题解
    New World
    CSP2019游记
    LOJ6052 DIV
    CF809E Surprise me!
    Luogu4548 歌唱王国
    Luogu4581 想法
    Note 5.26-5.28
    LOJ6519 魔力环
  • 原文地址:https://www.cnblogs.com/a1-code/p/6000185.html
Copyright © 2020-2023  润新知