[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)

[Python] Chapter 05-09 Others-Use of for statement and while statement properly-

So far, we have used for and while statements as repetitive syntaxes for control syntax. We have confirmed that the processing contents are the same for these two. Rewriting from a for statement to a while statement was also mentioned in the previous section.

However, this for statement and while statement can be used properly. This time, I will touch on how to use them properly.

Use a while statement to repeat an undetermined number of times

For example, if you are told ** "Put 10 glasses of water in a pot." **, you know that you will put 10 glasses of water. But what if you were told ** "Pour water in this cup until a pot is full" **? I don't know in advance how many glasses of water to put in at that point.

In this way, the while statement is suitable when you do not know the ** number of times in advance or when you write ** iterative processing without deciding the number of times.

For example, let's see an example in the following program. The contents of the program are as follows. "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. "

Create a file with the file name samp05-09-01.py </ font> in chap05 </ font>, and use the following code Please write.

samp05-09-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

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}')


samp05-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

I think there is no particular problem with the contents of the program. Looking at this, I don't know how many people are in the class, so at the beginning of the process, I'm adding one person at a time. However, at the time of exiting the while statement, the number of people has increased by one, so 1 is reduced.

Finally

Did you understand how to use the for statement and the while statement properly? It's common to use iterative syntax without knowing how many people or how many times it has been processed. It's a technique you use a lot, so be sure to keep it down.

Return to [Table of Contents Link]

Recommended Posts