To find the maximum value from a list of single positive integers of a file following procedure is used:
First open the file and initialize the maxvalue variable. Now, begin the for loop that will compare the maxvalue variable with the value stored in the file. If the value of maxvalue variable is less than the stored value of the file then store it in maxvalue variable otherwise not store it. The comparison will continue till the last value of the file and finally display the maximum value.
Further explanation:
The python code to calculate the maximum value from a file is as shown below:
Code:
#First open the file in the read mode
file = open('numbers.txt', 'r');
#Initialize the variable
maxvalue=0
#Begin the for loop.
for line in file:
#Compare the values of text file from maxvalue variable and find the maximum value
if maxvalue <= int (line):
#Store the maximum value in the variable maxvalue.
maxvalue = int (line)
#Display the maximum value of the numbers.txt file.
print ('maxvalue is:',maxvalue)
The content of the text file is as shown below:
<u>Text file "numbers.txt"</u>
3
6
8
1
6
9
4
7
2
Output:
maxvalue is : 9
Learn more:
1. How does coding work on computers? brainly.com/question/2257971
2. Prediction accuracy of a neural network depends on _______________ and ______________. brainly.com/question/10599832
Answer details:
Grade: College Engineering
Subject: Computer Science
Chapter: Python Programming
Keyword:
python, input, output, programming, statements, char, int, variables, maxvalue, open, file, loop, text file, line, numbers