Until now, I have written iterative processing using for statements and while statements. In fact, you can forcibly interrupt this process in the middle, or continue the process depending on the conditions. For that purpose, there are ** break processing ** and ** continue processing **, respectively.
Let's touch on those processes. Let's take a look while improving the program in Chapter 05-09. First, let's take a look at the program in Chapter 05-09 again. Check the problem again. "We will create a program to calculate the ** total and ** average ** of the test scores of the students in the class. However, the number of students in the class varies from class to class, so we do not know how many. Enter the points in order, and if a negative value is entered, create a program to calculate the total points and average points up to that point. The points are 100 points out of 100. "
i = 1 #i is the number of people in the class
total = 0 #Add the total points of the class to total
score = 0 #Student score
while score >= 0:
total += score
score = int(input(f'{i}Please enter the score of the person (the score is 100 points out of 100. Entering a negative value ends the process):'))
i += 1
i -= 1 #Since there is one more person, we will reduce it by one person
print(f'Total score:{total}')
print(f'Average score:{total/i}')
05-09-01.py Execution result
[Execution result] </ font> Please enter the score of the first person (the score is 100 points out of 100. The process ends when you enter a negative value): 90 Please enter the score of the second person (the score is 100 points out of 100. The process ends when you enter a negative value): 75 Please enter the score of the third person (the score is 100 points out of 100. The process ends when you enter a negative value): 83 Please enter the score of the 4th person (the score is 100 points out of 100. The process ends when you enter a negative value): -1 Total score: 248 Average score: 62.0
Looking at the above program earlier, at the end, there is ** "i-= 1 # We will reduce one person because there is one more person" **. For some people, I'm curious about subtracting this one. (It may reduce the readability of the program.) I would like to think of a way to eliminate this process.
Now, in the above program, it is determined whether to continue or end the score input process based on the condition of the ** while statement. ** ** It is also possible to perform input processing in the ** while statement **, and if the input value is a negative number, ** forcibly exit the while statement **. In that case, use ** break **.
Let's take a look at the following program. Create a file with the file name samp05-10-01.py </ font> in chap05 </ font>, and use the following code Please write.
samp05-10-01.py
i = 1 #i is the number of people in the class
total = 0 #Add the total points of the class to total
score = 0 #Student score
#Since the while condition is True, it will be an infinite loop as it is.
while True:
total += score
score = int(input(f'{i}Please enter the score of the person (the score is 100 points out of 100. Entering a negative value ends the process):'))
#If the input value score is a negative number, break exits the while statement.
if score < 0:
print(f'{score}Is entered, the while statement is forcibly exited.')
break
i += 1
#i -= 1 #← Processing here can be eliminated
print(f'Total score:{total}')
print(f'Average score:{total/i}')
samp05-10-01.py execution result
[Execution result] </ font> Please enter the score of the first person (the score is 100 points out of 100. The process ends when you enter a negative value): 90 Please enter the score of the second person (the score is 100 points out of 100. The process ends when you enter a negative value): 75 Please enter the score of the third person (the score is 100 points out of 100. The process ends when you enter a negative value): 83 Please enter the score of the 4th person (the score is 100 points out of 100. The process ends when you enter a negative value): -1 Since -1 was entered, the while statement is forcibly exited. Total score: 248 Average score: 62.0
First, there are two distinctive points. The first is the ** while True: ** section. Originally, comparison operators such as ** == ** and ** <= ** are used for this condition, but writing ** True ** has already written the result of the comparison operator. I will. Therefore, as it is, it is always ** True **, which means that it will be an infinite loop. I need to get out of it somehow. That method is the second feature described below.
The second is ** break ** inside the ** while statement ** and then inside the ** if statement **. Since the condition of the if statement is ** score <0 **, the condition "if a negative number is entered" is used here to pass ** break **. This ** break ** allows you to forcibly break out of the ** while statement **.
Then, the total value and average value of the points entered at the end are calculated. And you can eliminate the process of ** "i-= 1" ** that you wrote at the very beginning.
The program I wrote earlier has another drawback. Let's pay attention to the part ** "The score is 100 points out of 100." ** in the above question sentence. Now, when inputting, I was inputting so as not to exceed 100 points. However, since this score is entered by humans, it is possible to enter ** 900 ** by mistakenly typing 0 instead of entering ** 90 ** in the score.
If this program is a scoring program, it's still okay, but what if it's a money-handling system? It will be a serious incident (*) </ font>.
(*) </ font> ** Incident ** means an unexpected system interruption ** or quality degradation **. It has a similar meaning to trouble. It is a term often used in the field of IT service management.
There is a ** continue statement ** as a way to prevent this. I would like to improve the previous program further, but I will make it separately. In chap05 </ font>, create a file with the file name samp05-10-02.py </ font>, and the following code Please write. In addition, the addition of the entered points (** total + = score **) is rewritten so that it will be performed after two if judgments.
samp05-10-02.py
i = 1 #i is the number of people in the class
total = 0 #Add the total points of the class to total
score = 0 #Student score
#Since the while condition is True, it will be an infinite loop as it is.
while True:
score = int(input(f'{i}Please enter the score of the person (the score is 100 points out of 100. Entering a negative value ends the process):'))
#If the input value score is a negative number, break exits the while statement.
if score < 0:
print(f'{score}Is entered, the while statement is forcibly exited.')
break
#If the input value score is greater than 100, the process starts from the beginning of the while statement without counting the number of people i.
if score > 100:
print('A value greater than 100 has been entered for score, so enter it again.')
continue
total += score #This time, score addition processing after the above two if judgments
i += 1
#i -= 1 #← Processing here can be eliminated
print(f'Total score:{total}')
print(f'Average score:{total/i}')
samp05-10-02.py execution result
[Execution result] </ font> Please enter the score of the first person (the score is 100 points out of 100. The process ends when you enter a negative value): 90 Please enter the score of the second person (the score is 100 points out of 100. The process ends when you enter a negative value): 75 Please enter the score of the third person (the score is 100 points out of 100. The process ends when you enter a negative value): 833 A value greater than 100 has been entered for score, so enter it again. Please enter the score of the third person (the score is 100 points out of 100. The process ends when you enter a negative value): 83 Please enter the score of the 4th person (the score is 100 points out of 100. The process ends when you enter a negative value): -1 Since -1 was entered, the while statement is forcibly exited. Total score: 248 Average score: 62.0
In the ** if statement ** in the while statement, it is judged whether or not a value larger than 100 points is input at ** score> 100 **. And if a value greater than 100 is entered, the ** continue statement ** will start over from the beginning of the while statement.
This time I touched on ** break statements ** and ** continue statements **. Some programmers find it difficult to maintain these two because some people say that they are less mobile. At the actual development site, when using it, it will be necessary to proceed with development while discussing it at meetings.
Also, as I mentioned earlier, you may make a mistake in the input when executing the program. Actually, there is still something to be fixed even with the previous input, and if you enter a character string as well as a numerical range, an error will occur. You must also consider what to do when such an error occurs. It is good to remember that creating a mechanism that does not make such mistakes is called ** foolproof **. (It is often asked in the Information-Technology Engineers Examination)