python:while loop statement and exercises

Keywords: Python less Programming

while loop sentences and exercises

In Python programming, the while statement is used to execute a program circularly, that is, under certain conditions, to execute a certain program circularly to handle the same tasks that need to be handled repeatedly. Its basic form is: while judgment condition: execute statement... The execute statement can be a single statement or statement block. The judging condition can be any expression, and any non-zero or non empty (null) value is true.
When the condition is false, the loop ends.

Example:

count = 0
while (count < 9):
   print ('The count is:', count)
   count = count + 1
 
print("Good bye!")

Operation result:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

continue and break usage

In the while statement, there are two other important commands, continue and break, to skip the loop. Continue is used to skip the loop, and break is used to exit the loop. In addition, the judgment condition can be a constant value, indicating that the loop must be true. The specific usage is as follows:

i = 1
while i < 10:   
    i += 1
    if i%2 > 0:     # Skip output when not even
        continue
    print(i)         # Output double2,4,6,8,10

Operation result:

2
4
6
8
10

i = 1 while 1: # The cycle condition is1Must be established print(i) # output1~10 i += 1 if i > 10: # When igreater than10Jump out of cycle break

Operation result:

1
2
3
4
5
6
7
8
9
10

Infinite cycle

If the conditional statement is always true, the loop will execute indefinitely, as shown in the following example:

var = 1
while var == 1 :  # The condition is alwaystrue,The loop will continue indefinitely
   num = input("Enter a number  :")
   print ("You entered: ", num)
 
print ("Good bye!")
Enter a number  :3
You entered:  3
Enter a number  :4
You entered:  4
Enter a number  :5
You entered:  5
Enter a number  :6
You entered:  6
Enter a number  :7
You entered:  7
Enter a number  :8
You entered:  8
Enter a number  :100
You entered:  100
Enter a number  :Traceback (most recent call last):
  File "c:/1.py", line 76, in <module>
    #    num = input("Enter a number  :")
KeyboardInterrupt

Note: for the above infinite loop, you can use CTRL+C to interrupt the loop.

Recycle else statements

In python, while Else executes the else statement block when the loop condition is false:

count = 0
while count < 5:
   print (count, " is  less than 5")
   count = count + 1
else:
   print (count, " is not less than 5")

Operation result:

0  is  less than 5
1  is  less than 5
2  is  less than 5
3  is  less than 5
4  is  less than 5
5  is not less than 5

Related exercises

1. Output 1,2,3,4,5,6,8,9,10
count = 1
while count <= 10:
  if count == 7:
    pass
  else:
    print(count)
  count = count + 1
2. Output 1-100 sum of all numbers
n = 1
m = 0 
while n < 101:
  m = m + n
  n = n + 1
print(m)
3. Output all odd numbers within 100
n = 1
while n < 101:
  temp = n % 2
  if temp == 0:
    print(n)
  else:
    pass
  n = n +1
4. Output 1-2 + 3-4 + 5..... 99 sum of all numbers
n = 1
m = 0 
while n < 100:
  temp = n % 2
  if temp == 0:
    m = m - n
  else:
    m = m + n
  n = n + 1
print(m)

reference material:

1.Python While loop sentence novice tutorial: https://www.runoob.com/python/python-while-loop.html

Posted by johnrcornell on Sat, 02 Nov 2019 22:32:44 -0700