Jul-17-2019, 03:18 PM
(Jul-17-2019, 02:41 PM)perfringo Wrote: I observe that this is second thread on same topic.
There is no information about file format available but my random guess is:
with open(“nums.txt”, “r”) as f: numbers = [int(num) for num in f.read().split()]
Sry changed the question from the other, ill be moving back to that topic soon about the IOERRORS.
(Jul-17-2019, 02:54 PM)ichabod801 Wrote: Okay, then you just need to split by lines. The code perfringo posted is one way to do that. Another way, is to just loop over the file:Works perfectly
with open('nums.txt') as nums_file: numbers = [] for line in nums_file: numbers.append(int(line))Or you can do a list comprehension like perfringo did.
with open('nums.txt') as nums_file: numbers = [] for line in nums_file: numbers.append(int(line)) print("Total =", sum(numbers)) def Average(numbers): return sum(numbers) / len (numbers) print("Average =", round(Average(numbers), 2))