"Python programming from introduction to practice" -- while loop

  the for loop is used to create a code block for each element in the collection, and the while loop runs continuously until the specified conditions are not met. General form of while statement in Python:
  while judgment condition:
    execute statements
  also pay attention to colons and indents. In addition, there is no do... while loop in Python.

# input
counter_number = 100
sum_value = 0
i = 0
while i <= counter_number:
    sum_value = sum_value + i
    i = i +1
print('The sum is: {0:10}'.format(sum_value))

# output
The sum is:       5050

  loop exit: the user selects to decide whether to exit.
   the example below also introduces flag to judge the condition judgment. Flags are useful: the loop exits when any of these events causes the activity flag to become False.

# input
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    sum_value = sum_value + i
    i = i +1
    if (i == 50):
        input_value = input(quit_message)
        if input_value.upper() == "Y":
            flag = False
    
print('The sum is: {}'.format(sum_value))

# output
The value is reached the max, whether select quit. Please input Y/N to select:Y
The sum is: 1225

  loop exit: break to exit the loop.
  to exit the while loop immediately, do not run the remaining code in the loop, and use the break statement regardless of the result of the conditional test. Break statement is used to control the program flow. You can use it to control which code lines are executed and which code lines are not executed, so that the program can execute the code you want to execute according to your requirements. You can use the break statement in any Python loop

# input
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    sum_value = sum_value + i
    i = i +1
    if (i == 50):
        break
    
print('The sum is: {}'.format(sum_value))

# output
The sum is: 1225

  loop interrupt: continue interrupts the loop.
    to return to the beginning of the loop and decide whether to continue the loop according to the conditional test results, you can use the continue statement, which does not execute the remaining code and exit the whole loop like the break statement. The following example is the sum of all even numbers between 1 and 100.

# input
counter_number = 100
sum_value = 0
i = 0
flag = True
quit_message = "The value is reached the max, whether select quit. Please input Y/N to select:"
input_value = ""
while ((i <= counter_number)and(flag)):
    i = i +1
    if (i%2 == 0):
        sum_value = sum_value + i
        continue 
print('The sum is: {}'.format(sum_value))

# output
The sum is: 2550

  to avoid writing infinite loops, be sure to test each while loop to ensure that it ends as expected. If you want the program to end when the user enters a specific value, run the program and enter such a value; If the program does not end in this case, check the way the program handles this value and make sure that the program has at least one place where the loop condition is False or the break statement can be executed. Some editors (such as Sublime Text) have embedded output windows, which may make it difficult to end the infinite loop, so you have to close the editor to end the infinite loop.






  use the while loop to process lists and dictionaries
  the for loop is an effective way to traverse the list, but the list should not be modified in the for loop, otherwise it will make it difficult for Python to track the elements in it. To modify the list while traversing it, use the while loop. By combining while loops with lists and dictionaries, you can collect, store, and organize large amounts of input for later viewing and display.

# Add element to list
# input
users = []
flag = True
input_value = ''
while (flag):
    input_value = input("Input the user name: ")
    users.append(input_value)
    print(users[:])
    if 'Youth' in users:
        flag = False

# output
Input the user name: Tom
['Tom']
Input the user name: Jerry
['Tom', 'Jerry']
Input the user name: Hank
['Tom', 'Jerry', 'Hank']
Input the user name: Youth
['Tom', 'Jerry', 'Hank', 'Youth']
# Delete element
# input
while 'Tom' in users:
    users.remove('Tom')
print(users)

# output
['Jerry', 'Hank', 'Youth']

  the while loop can be used to prompt the user to enter any number of information. We can store the information entered by the user in the dictionary.

# input
users = {}
flag = True
quit_message = "Please select whether continue(Y/N): "
input_value = ''
while (flag):
    name = input("Input the user name: ")
    age = input("Input the user age: ")
    users[name] = age
    input_value = input(quit_message)
    if input_value.upper() == 'N':
        flag = False
print(users)


# output
Input the user name: Tom
Input the user age: 15
Please select whether continue(Y/N): Y
Input the user name: Jerry
Input the user age: 25
Please select whether continue(Y/N): N
{'Tom': '15', 'Jerry': '25'}

Keywords: Python

Added by horsefaceba on Wed, 12 Jan 2022 08:34:03 +0200