Python--003--Process Control while,for

Source: Internet
Author: User

Process Control
Sequential processes: Each line of code executes and is executed only once
···
If--Else struct statement:
1. If Boolean expression:
Code alignment, tab and space can not appear at the same time

2. If Boolean expression:
Code
Else

Money = input (' Please input your money: ')
If Money > 100:
print ' You're vip! '
print ' You are Gold VIP '
else:
Print (' You're not vip! ')

3, If--else nested:
An If Boolean expression:
Code
Else
An If Boolean expression:
Code
Else
Code
....

Money = input (' Please input your money: ')
If Money > 100:
print ' You're vip! '
If Money >= 200:
print ' You're golden VIP '
If 200>money>100:
print ' You're a silver VIP '
else:
If money<0:
print ' error! '
Else
Print (' You're not vip! ')

4. If Boolean expression:
Code
elif Boolean expression:
Code

Money = input (' Please input your money: ')
If Money > 100:
print ' You're vip! '
If Money >= 200:
print ' You're golden VIP '
If 200>money>100:
print ' You're a silver VIP '
Elif money<0:
print ' error! '
Else
Print (' You're not vip! ')

EG:
The first type:
#coding: UTF-8
score = Int (input (' Please input a score: '))

If 100>=score>=90:
Print (' very good ')
If 90>=score>=70:
Print (' Well done! ')
If 70>=score>=60:
Print (' pass! ')
If 60>score:
Print (' failed! ')
If score>100 or score<0:
Print (' error! ')

The second type:

- -coding:cp936--

score = Int (input (' Please input a score: '))

If 100>=score>=90:
Print (' very good ')
Else
If 90>=score>=70:
Print (' good ')
Else
If 70>=score>=60:
Print (' Pass ')
Else
If 60>score>=0:
Print (' fail ')
Else
Print (' error! ')
The third type:
score = Int (input (' Your number: '))
If 100>=score>=90:
Print (' excellent ')
Elif 90>=score>=70:
Print (' good ')
Elif 70>=score>=60:
Print (' Pass ')
Elif 60>score>=0:
Print (' Flunk ')
Else
Print (' error! ')

Cycle flow
·· While loop

Run some code repeatedly
1. Syntax:
While Boolean expression:
Code: (Loop body)
Note: To avoid a dead loop (notice that the loop jumps out of the condition)
Server is a dead loop7 365
num = 0
While num<10:
print ' Hello world! '
Num+=1

····· For loop
1. Syntax:
For-Target in expression:
Loop body

                            目标:变量                            表达式:字符串、列表、元祖----可迭代对象

A = ' Baizhi '
For I in A:
Print I

Teacher = [' Fei ', ' xige ', ' Kuai ']
For I in teacher:
Print I

Number = [123,466,969]
For I in number:
Print I

Range ()
Grammar:
Range ([Start],stop[,step=1])
1. This function has three parameters, where square brackets indicate selectable parameters ()
2, the first parameter default is 0, the second parameter stop, the third default 1
3, the function generates a value, starting from start, to stop ending the number sequence
Value range from [Start,stop] left closed right open

A = range (0,3,1)

>> A
[0, 1, 2]
>>
>> A=range (0,10,2)
>> A
[0, 2, 4, 6, 8]
>> a = range (0,10,3)
>> A
[0, 3, 6, 9]
>> Range (3)
[0, 1, 2]

For I in range (5):
Print I

01
2
3
4

>> for I in range (2,5):
Print I

2
3
4

>> for I in Range (1,5,2):
Print I

1
3

·· Break
Jumps out of the current loop, not all subsequent loops

While True:
num = input (' Please input your number: ')
If num ==1:
Break
Print (' Error,try again! ')

Please input your number:3
Error,try again!
Please input your number:1

continue
Skip this cycle without affecting the next loop

For I in range (10):
If i%2!=0:
Continue
Print I

02
4
6
8

>> for I in range (10):
If i%2!=0:
Break
Print I

Python--003--Process Control while,for

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.