Python full stack development "fourth" Python Process Control

Source: Internet
Author: User

12 If...else of Process Control
既然我们编程的目的是为了控制计算机能够像人脑一样工作,那么人脑能做什么,就需要程序中有相应的机制去模拟。人脑无非是数学运算和逻辑运算,对于数学运算在上一节我们已经说过了。对于逻辑运算,即人根据外部条件的变化而做出不同的反映,比如1 如果:女人的年龄>30岁,那么:叫阿姨

2 if: Woman's age >30 years old, then: Call aunt, otherwise: called Miss

  


3 if: Woman's age >=18 and <22 years old and height >170 and weight <100 and is beautiful, then: vindicate, otherwise: call aunt

Age_of_girl=18 height=171 weight=99 is_pretty=true if Age_of_girl >= and Age_of_girl < and height > Weight < Is_pretty = = True:print (' confession ... ') else:print (' Aunt Good ') continues on the basis of confession: If the confession succeeds, then: Together otherwise: print ... Age_of_girl=18 height=171 weight=99 is_pretty=truesuccess=falseif age_of_girl >= and Age_of_girl < and height > Weight < and Is_pretty = = True:if Success:print (' vindicate success, together ') Else:print (' What Love does not love, Love NMLGB love, Love nmlg ah ... ') ) Else:print (' Auntie Good ')

  

4 如果:成绩>=90,那么:优秀   如果成绩>=80且<90,那么:良好   如果成绩>=70且<80,那么:普通   其他情况:很差
Score=input (' >>: ') Score=int (score) if score >= 90:print (' excellent ') Elif score >= 80:print (' good ') Elif score >= 70:print (' normal ') else:print (' very poor ') if condition 1: Indented code block elif Condition 2: Indented code block elif Condition 3: Indented code block ... else: indented block of code

  

!/usr/bin/env pythonname=input (' Please enter user name: ') password=input (' Please enter password: ') if name = = ' Egon ' and password = = ' 123 ': Print (' Egon Login Success ') else:print (' User name or password error ')
Practice One: User Login verification

  

#!/usr/bin/env python# Print Its permissions according to user input "' Egon---Super admin tom--  general Administrator Jack,rain--Business supervisor Other--ordinary user ' name=  Input (' Please enter user name: ') if name = = ' Egon ':    print (' Super admin ') elif name = = ' Tom ':    print (' normal admin ') elif name = = ' Jack ' or name = = ' Rain ':    print (' Business Supervisor ') Else:    print (' normal user ') Exercise two: Export its permissions based on user input

# if: Today is Monday, then: To work # if: Today is Tuesday, then: To work # if: Today is Wednesday, then: To work # if: Today is Thursday, then: Work # if: Today is Friday, then: Work # if: Today is Saturday, then: Go out Wave # if: Today is Sunday, then: Go out Wave # Way one: Today=input (' >>: ') if today = = ' Monday ': print (' work ') elif today = = ' Tu Esday ': Print (' work ') elif today = = ' Wednesday ': print (' work ') elif today = = ' Thursday ': print (' work ') elif today = = ' Frid Ay ': print (' work ') elif today = = ' Saturday ': print (' Out of the Waves ') elif today = = ' Sunday ': print (' Out of the Waves ') else:print (' must enter its  Medium: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ") #方式二: Today=input (' >>: ') if Today = = ' Saturday ' or today = = ' Sunday ': print (' Out of the Waves ') elif today = = ' Monday ' or today = = ' Tuesday ' or today = = ' Wednes Day ' or today = = ' Thursday ' or today = = ' Friday ': print (' work ') else:print ("' must enter one of them: Monday Tuesday We Dnesday Thursday Friday Saturday Sunday ") #方式三: Today=input (' >>: ') if today in [' Saturday ', ' Sunday '] : Print (' Out of the Waves ') elif today in [' MoNday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ': print (' work ') else:print ("' must enter one of them: Monday Tuesday Wednes Day Thursday Friday Saturday Sunday ") Exercise three

  

13 Process Control While loop
1 为何要用循环

Last lesson we have learned to use if. Else to guess the age of the game, but only to guess the odds are too small, if I want to give the player 3 chance? Is the program starts, the player can try up to 3 times, this How to do? You don't want to copy the code 3 times ....

Age_of_oldboy = 48

guess = Int (input (">>:"))

If guess > Age_of_oldboy:
Print ("Guess too big, try it on the small ...")

Elif Guess < age_of_oldboy:
Print ("Guess too small, try the Dali ...")

Else
Print ("Congratulations, guess right ...")

2nd time
guess = Int (input (">>:"))

If guess > Age_of_oldboy:
Print ("Guess too big, try it on the small ...")

Elif Guess < age_of_oldboy:
Print ("Guess too small, try the Dali ...")

Else
Print ("Congratulations, guess right ...")

3rd time
guess = Int (input (">>:"))

If guess > Age_of_oldboy:
Print ("Guess too big, try it on the small ...")

Elif Guess < age_of_oldboy:
Print ("Guess too small, try the Dali ...")

Else
Print ("Congratulations, guess right ...")

Even small white you, also feel too low is not, later to modify the function also have to modify 3 times, so remember, write duplicate code is the most shameful behavior of the programmer.
So how do you do not have to write duplicate code and let the program repeat a piece of code multiple times? The loop statement comes in handy.

2 条件循环:while,语法如下

While condition:
Loop body

 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。 如果条件为假,那么循环体不执行,循环终止

Printing 0-10
Count=0
While Count <= 10:
Print (' Loop ', count)
Count+=1

Print an even number between 0-10
Count=0
While Count <= 10:
If count%2 = = 0:
Print (' Loop ', count)
Count+=1

Print an odd number between 0-10
Count=0
While Count <= 10:
if count%2 = = 1:
Print (' Loop ', count)
Count+=1

3 死循环

Import time
Num=0
While True:
Print (' count ', num)
Time.sleep (1)
Num+=1
4 loop nesting and tag

Tag=true

While tag:

......

While tag:

........

While tag:

Tag=false

Practice, the requirements are as follows:
1 loop to verify user name and password entered by user
2 After the authentication is passed, run the user to execute the command repeatedly
3 exit the entire program when the user input command is quit

Implement one:
Name= ' Egon '
Password= ' 123 '

While True:
Inp_name=input (' User name: ')
Inp_pwd=input (' Password: ')
If inp_name = = Name and Inp_pwd = = password:
While True:
Cmd=input (' >>: ')
If not cmd:continue
if cmd = = ' Quit ':
Break
Print (' Run <%s> '%cmd)
Else
Print (' User name or password error ')
Continue
Break

Implementation two: Using tag
Name= ' Egon '
Password= ' 123 '

Tag=true
While tag:
Inp_name=input (' User name: ')
Inp_pwd=input (' Password: ')
If inp_name = = Name and Inp_pwd = = password:
While tag:
Cmd=input (' >>: ')
If not cmd:continue
if cmd = = ' Quit ':
Tag=false
Continue
Print (' Run <%s> '%cmd)
Else
Print (' User name or password error ')

4 break与continue

Break to exit this layer loop
While True:
Print "123"
Break
Print "456"

Continue is used to exit this loop and continue the next cycle
While True:
Print "123"
Continue
Print "456"

5 while+else

Unlike other languages else, it is generally only the same as if, and in Python there is a while ... else statement, while the else function behind the while loop is that the statement after the else is executed when the while cycle is done properly and the middle is not aborted
Count = 0
While Count <= 5:
Count + = 1
Print ("Loop", Count)

Else
Print ("Loop normal execution")
Print ("-----out of a while loop------")
Output
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
The loop is running, it's done.
-----out of a while loop------

If it is break during execution, then the Else statement is not executed.
Count = 0
While Count <= 5:
Count + = 1
if count = = 3:break
Print ("Loop", Count)

Else
Print ("Loop normal execution")
Print ("-----out of a while loop------")
Output

Loop 1
Loop 2
-----out of a while loop------

6 while循环练习题
    1. Using while loop output 1 2 3 4 5 6 8 9 10
    2. All numbers of the 1-100 are calculated and
    3. All odd numbers within the output 1-100
    4. All even numbers in the output 1-100
    5. Beg 1-2+3-4+5 ... 99 of all numbers of the and
    6. User Login (three chance retry)
      7: Guess Age Game
      Requirements:
      Allow users to try up to 3 times, 3 times without guessing the right, then directly exit, if guessed right, print congratulations message and exit
      8: Guess Age Game upgrade version
      Requirements:
      Allow users to try up to 3 times
      After each attempt 3 times, if not guessed right, ask the user whether still want to continue to play, if the answer Y or Y, continue to let it guess 3 times, to reciprocate, if answer n or N, quit the program
      How to guess right, just quit.

Question One
Count=1
While Count <= 10:
if Count = = 7:
Count+=1
Continue
Print (count)
Count+=1

Count=1
While Count <= 10:
If count! = 7:
Print (count)
Count+=1

Topic Two
Res=0
Count=1
While Count <= 100:
Res+=count
Count+=1
Print (RES)

Topic Three
Count=1
While Count <= 100:
If count%2! = 0:
Print (count)
Count+=1

Topic Four
Count=1
While Count <= 100:
If count%2 = = 0:
Print (count)
Count+=1

Topic Five
Res=0
Count=1
While Count <= 5:
If count%2 = = 0:
Res-=count
Else
Res+=count
Count+=1
Print (RES)

Topic Six
Count=0
While Count < 3:
Name=input (' Please enter user name: ')
Password=input (' Please enter password: ')
If name = = ' Egon ' and password = = ' 123 ':
Print (' Login success ')
Break
Else
Print (' User name or password error ')
Count+=1

Topic Seven
age_of_oldboy=73

Count=0
While Count < 3:
Guess=int (Input (' >>: '))
If guess = = Age_of_oldboy:
Print (' You Got it ')
Break
Count+=1

Topic Eight
age_of_oldboy=73

Count=0
While True:
if Count = = 3:
Choice=input (' Continue ' (y/n?) >>: ')
if choice = = ' Y ' or choice = = ' Y ':
Count=0
Else
Break

guess=int(input(‘>>: ‘))if guess == age_of_oldboy:    print(‘you got it‘)    breakcount+=1
14 flow control for the for loop

1 iterative loops: For, the syntax is as follows
For I in range (10):
Indented block of code
2 Break and Continue (IBID.)
3 Loop nesting
For I in Range (1,10):
For j in Range (1,i+1):
Print ('%s%s=%s '% (i,j,ij), end= ")
Print ()
For+else

Python full stack development "fourth" Python Process Control

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.