I need help with my Python Program!!!! You’ll get N numbers from a file, store t
ID: 3779503 • Letter: I
Question
I need help with my Python Program!!!!
You’ll get N numbers from a file, store the N numbers in an array, find the largest number, print the largest number to a different file or append to the original file, and print the list/array of numbers to the screen for the user.
*You need to create the input file with N numbers.
*You will read the input file name (and possible an output file, if you are writing the largest number to a different file, rather than appending to the input file) as a command line argument(s).
Explanation / Answer
Here is the code for you:
#!/usr/bin/python
#You'll get N numbers from a file, store the N numbers in an array
with open("numbers.txt", "r") as ins:
array = []
for line in ins:
array.append(line)
#find the largest number
largest = array[0]
for i in range(len(array)):
if array[i] > largest:
largest = array[i]
#print the largest number to a different file or append to the original file
append = open('numbers.txt', 'a') #Opens a file numbers.txt in write mode.
append.write(' ')
append.write(str(largest))
#print the list/array of numbers to the screen for the user.
outF = open('numbers.txt', 'r') #Opens a file numbers.txt in write mode.
print outF.read()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.