当前位置:网站首页>[section 5 if and for]

[section 5 if and for]

2022-04-23 15:48:00 qq_ twenty-four million four hundred and nine thousand nine hun

The first 5 section if and for

if

"""if  Test login cases   If   user name  = “yuze”:  Login successful   If   user name  =  empty :  Please enter a user name   otherwise :  Login failed  if  Of   How to write it : if  Conditional expression : ( Indent )  The program that will be executed when the conditions are met   Multiline program   Conditional expression : The value to be obtained is a bool type , There is one condition  True ,False  If the condition holds , Sub code block execution , If not ,: The following subcode does not execute  if : pass else: pass if ( Conditional expression ): pass elif( Conditional expression ): pass elif( Conditional expression ): pass ... else; pass TODO: In the same  if .... elif ...else  In the condition , As long as one of the branches is executed , Other branches will no longer be executed  TODO: however , If it's different if if ...elif ...if... elif TODO:else  You can omit  if :... TODO: if: if: if: else: pass """
#if ...elif...else...
username  = "abc"
if username == "yuze" or username == "Demons":
    # Indentation indicates the sub code I need to execute after the conditions are met , It's a branch 
    print(" Login successful ")
elif username == "":
    print(" Please enter a user name ")
elif username =="Demons":
    print(" There is no such user ")
else:
    print(" Login failed ")
print(" After the condition ")

Multiple if

username  = "abc"
password = ""
gender = " Unknown "

if username == "yuze":
    print(" The user name is correct ")
    if password ==  "123456":
        print(" The password is correct ")
        if  gender == " male ":
            print(" Gender meets the requirements ")
        else:
            print(" Gender does not qualify ")
    elif password =="":
        print(" The password is empty. ")
    else:
        print(" Wrong password ")
else:
    print(" Incorrect user name ")

# Try to avoid multiple nesting 
if username == "yuze" and password == "123456" and gender == " male ":
    print(" Login successful ")
elif username =="yuze" and password =="":
    print(" The password is empty. ")

Not the same if sentence

# Not the same if sentence 
username  = "Demons"
if username == "yuze" or username == "Demons":
    # Indentation indicates the sub code I need to execute after the conditions are met , It's a branch 
    print(" Login successful ")
elif username == "":
    print(" Please enter a user name ")

if username =="Demons":
    print(" There is no such user ")
else:
    print(" Login failed ")
print(" After the condition ")

while loop

""" while  It's not very much  """

# username = ""
#
# while username =="yuze":
# print("hello world")
# print("finished")

# Dead cycle 
# username = ""
#
# while username !="yuze":
# print("hello world")
# # Manually force the exit of the whole while loop 
# break
# print("finished")
#
# username = ""
#
# while username !="yuze":
# print("hello world")
# # Enter the next cycle , Into the next loop 
# continue
# # print("continue  The following code is not executed ")
# print("finished")
#

#double king
times = 0
while times  <999:
    print(f" Have I said {
      times} Time ")
    print(" I like Xiaowen ")
    times = times+1
print(" The rich get married ")

for loop

"""for  I finished all the test cases  cases = [ {"username":"yuze","password":""}, {"username":"xianrenqiu","password":"123456"}, {"username":"","password":""} ] """
cases = [
    {
    "username":"yuze","password":""},
    {
    "username":"xianrenqiu","password":"123456"},
    {
    "username":"","password":""}
]

# hold cases  Each of these elements is executed once 
# Set value  {"username":"yuze","password":""}, Assign to case
#case It's a variable 
for case in cases:
    #case = cases[0]
    print(case)
    print(" Executing use case ")
    # When for  After the loop executes a sub loop 
    # Implicit steps , Indexes  index+=1
# Go through debugging debug
# Is to pause the running of the code to get the current data state 
# Breaking point , Tell the program where I should pause 

for nesting

a = [
    ["a","b","c"],
    [1,2,3],
]

# Nested list 
# Finish the inner layer first  for  loop  , Then execute the outer layer 
# for i in a:
# for j in i:
# print(j)


#for  A nested loop 
li =[1,2,3,4,5,6]
for i in li:
    for j in li:
        print(f"{
      i}*{
      j}={
      i*j}",end = "\t")

版权声明
本文为[qq_ twenty-four million four hundred and nine thousand nine hun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231545518326.html