Python development [Part 3]: branch loop

Keywords: Python less

1. if condition statement

Syntax:

if conditions:      
    Code block - true execution
 else: optional
    Code block condition is false execution

Example:

n = int(input('Please enter a number:'))
if n > 0:
    print('%s Greater than 0' % n)
else:
    print('%s Less than 0' % n)

The   if statement supports nesting:

if conditions:
    if conditions:
        Code block
    else: 
        Code block
else: 
    Code block

if - elif - else

When there are multiple conditions, it is not so convenient to always use if to judge. In order to be lazy, we introduced elif, the abbreviation of if - else.

score = int(input('Please enter a score:'))
if 100 >= score >= 90:
    print('A')
if 90 > score >= 80:
    print('B')
if 80 > score >= 60:
    print('C')
if 60 > score >= 0:
    print('D')
if score < 0 or score > 100:
    print('Input error')

                   

score = int(input('Please enter a score:'))
if 100 >= score >= 90:
    print('A')
elif 90 > score >= 80:
    print('B')
elif 80 > score >= 60:
    print('C')
elif 60 > score >= 0:
    print('D')
else:
    print('Input error')

2. while loop statement

If the   condition is true, the loop body always executes.

Syntax:

while condition:
    Circulatory body

Dead cycle:

while True:
    print('Dead cycle')

Example:

count = 0
while count < 10:       # count less than 10, loop until it is greater than 10, exit loop
    print('hello')
    count += 1
print('ok')

The     while loop statement can also have else's identity:

number = 23
running = True

while running:
    guess = int(input('enter a integer: '))

    if guess == number:
        print('congratulations,you guessed it!')
        print('but,you do not win any prizes!')
        running = False       # Loop ends here, jumps out of loop, and executes else

    elif guess < number:
        print('no,it is a litter higher than that')

    else:
        print('no, it is a litter lower than that')

else:
    print('The while loop is over')
    
print('Done!')

3. for loop statement

                     .

Syntax:

for i in 'she':
    print(i)
s
h
e

The   range ([start,] stop [, step = 1]) function can be used to create a list of integers, which is often used with for statements.

>>> s = range(5)        # Generate a list of integers 0 - 5
>>> type(s)
<class 'range'>
>>> list(s)
[0, 1, 2, 3, 4]

for i in range(3):
    print(i)
0
1
2

4. break statement

The function of   break statement is to terminate the loop and exit the loop.

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:      # Exit loop when n = 2
        break
    print(n)

1

5. continue statement

The    continue statement is used to terminate this cycle, continue the next cycle, and determine the cycle conditions before the next cycle.

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:      # When n is even, terminate this cycle and continue the next cycle
        break
    print(n)

1,3,5,7,9

6. exercise questions

                      

n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n += 1

   2. Calculate the sum of 1 - 100

n = 1
sum = 0
while n < 101:
    sum += n
    n += 1
print(n)

   3. Calculate the sum of 1-2 + 3-4 + 5-6... 99

n = 1
sum = 0
while n < 100:
    temp = n % 2
    if temp == 0:
        sum -= n
    else:
        sum += n
    n += 1
print(sum)

   4. Calculate the sum of all even numbers 1-100

n = 1
while n < 101:
    if n % 2 == 0:
        sum += n
    else:
        pass        # pass means that the code is not executed.
    n += 1
print(sum)

   5. User login (three retries)

count = 0
while count < 3:
    user = input('Please enter your user name:')
    psd = input('Please enter your password:')
    if user == 'Alina' and psd == '123456':
        print('Welcome back %s' % user)
    else:
        print('Input error, please try again')
    count += 1

Posted by scald on Fri, 06 Dec 2019 14:18:16 -0800