Python's Path -03-python syntax

Source: Internet
Author: User

3.1 If process judgment

3.1.1 If Else process judgment

#! Author:lanhan
_username = ' Lanhan '
_password = ' 123 '
Username = input ("username:")
Password = input ("password:")
if username = = _username and Password = = _password:
Print ("Welcome user {Name} login ...". Format (name=username))
Else:
Print ("Invalid username or password!")

3.1.2if...elif...else process judgment

#! Author:lanhan
Age_of_oldboy = 56
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got it.")
elif guess_age > Age_of_oldboy:
Print ("think smaller ...")
Else:
Print ("Think bigger!:")

3.2 While process judgment

3.2.1while True

Example 1:

#! Author:lanhan
Count = 0
While True:
Print ("Count:", Count)
Count = Count +1
if count = = 3:
Break

Example 2:

#! Author:lanhan
count = 0
Age_of_oldboy = A.
while True :
      if count = = 3:
         break
     guess_age = Int (input ( "Guess Age:" )
     if guess_age = = Age_of_oldboy:
         print ( "Yes,you got it." )
         break
     elif guess_age > Age_of_oldboy:
         Print ( "think smaller ..." )
     else :
          print ( "Think bigger!:" )
     Count +=1

3.2.2while...else

Example 1:

#! Author:lanhan
Count = 0
Age_of_oldboy = 56
whileCount < 3:
guess_age = Int (input ("Guess Age:"))
ifGuess_age = = Age_of_oldboy:
Print"Yes,you got it.")
Break# # # # #不往下走了, jump right out of the loop
elifGuess_age > Age_of_oldboy:
Print"Think smaller ...")
Else:
Print"Think bigger!:")
Count +=1
Else:
Print"You have tried too many times. Fuck off ")

3.3 For process judgment

3.3.1 for i in variable

Example 1:

#! Author:lanhan
for I in range (0,10,2):
If i < 3:
Print ("loop", i)
Else :
Continue # # # #跳出本次循环, continue the next cycle
Print ("hehe ...")

3.3.2for...else

Example 1:

#! Author:lanhan
Count = 0
Age_of_oldboy = 56
for I in range (3):
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got it.")
Break
elif guess_age > Age_of_oldboy:
Print ("think smaller ...")
Else:
Print ("Think bigger!:")
Else:
Print ("You have tried too many times. Fuck off ")

Python's Path -03-python syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.