006-python basis-conditional judgment and circulation

Source: Internet
Author: User

first, condition judgment

Scenario one, User Login Verification

1 #prompt to enter user name and password2   3 #Verify user name and password4 #if error, the output user name or password is incorrect5 #if successful, the output is welcome, xxx!6  7 #!/usr/bin/env python8 #-*-coding:encoding-*-9   Ten ImportGetpass one    aName = Raw_input ('Please enter user name:') -PWD = Getpass.getpass ('Please enter your password:') -    the ifName = ="Alex"  andPWD = ="cmd": -     Print("welcome, alex!. ") - Else: -     Print("incorrect user name and password")

Scenario two, guess age Game

Set your age in the program, and then start the program to let the user guess, after the user input, according to his input prompts the user to enter the correct, if the error, the hint is guessed big or small

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3  4My_age = 285  6user_input = int (input ("input your guess num:"))7  8 ifUser_input = =my_age:9     Print("congratulations, you got it!")Ten elifUser_input <my_age: one     Print("Oops,think bigger!") a Else: -     Print("Think smaller!")
second, for Loop

The simplest loop 10 times

1 # _*_coding:utf-8_*_ 2 __author__ ' Alex Li ' 3  4  5  for  in range:6     print("loop:", I

Requirements One: or the above program, but encountered less than 5 cycle times will not go, jump directly into the next cycle

1  for  in range:2     if i<5:3         continue # without going down, go straight to the next loop. 4     Print ("loop:", I)

Demand Two: or the above program, but encountered more than 5 cycle times will not go, direct exit

1  for  in range:2     if i>5:3break         #  Don't go down, just jump out of the loop4     print("loop:", I )
third, while loop

There is a cycle called the cycle of death, once triggered, will run an everlasting, lasting.

1 count = 02 while True:3     print("  You are the wind I am the sand, wrapped around the horizon ... " , Count) 4     Count +=1

The code above loops out 100 times.

1Count =02  whileTrue:3     Print("You are the wind I am the sand, wrapped around the horizon ...", Count)4Count +=15     ifCount = = 100:6         Print("go to your mother's wind and sand, you these pants are people, put on pants are ghosts of the smelly man .")7          break

Back to the example above for the loop, how to enable the user to constantly guess age, but only give up to 3 times, then guess the wrong to quit the Program.

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3  4  5My_age = 286  7Count =08  whileCount < 3:9user_input = int (input ("input your guess num:"))Ten   one     ifUser_input = =my_age: a         Print("congratulations, you got it!") -          break -     elifUser_input <my_age: the         Print("Oops,think bigger!") -     Else: -         Print("Think smaller!") -Count + = 1#Each loop counter +1 + Else: -     Print("guess it's wrong so many times, you idiot.")
Iv. about break and continue

Break is used to exit all LOOPS.

Note: break only jumps out of a loop, if there are two loops, the second loop is nested inside the first loop, and if the second loop is break, the first loop will continue to Execute.

1  while True: 2     Print ("123") 3      break 4     Print ("456")

Continue is used to exit the current loop and continue the next loop

1  while True: 2     Print ("123") 3     Continue 4     Print ("456")
V. Relationship of the same level while, for and else

The else is executed if the number of times it is set is not interrupted by break in the while and for normal loop, but if the middle is broken, the Else statement will not be Executed.

When the normal cycle ends:

1Flag_exit =False2  while  notflag_exit:3     Print("123")4     Print("456")5Flag_exit =True6 Else:7     Print("Bye")8 9 #OutputTen123 one456 aBye # after the normal loop is finished, execute the statement under Else.

Break Exit Situation:

1Flag_exit =False2  while  notflag_exit:3     Print("123")4     Print("456")5      break6 Else:7     Print("Bye")8 9 #OutputTen123 one55W

006-python basis-conditional judgment and circulation

Related Article

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.