-4

I've created a program that generates a list of numbers of users choice, between 1 and 500. The program then writes the list to a file, reads the file, and lists all the numbers. I need to sum the numbers and show the count. Here is my code...

numberFile = open("random_number.txt", "w" )

for i in range(int(input("How many random numbers?: "))):
   numbers = str(randint(1, 500))
   numberFile.write(numbers)
   print(numbers)


numberFile.close()

Any help is greatly appreciated.

  • Possible duplicate of [Sum a list of numbers in Python](https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python) – Irfanuddin Oct 18 '18 at 18:17
  • Where is the summation happening ? I come from a JAVA background and so I am not familiar with python syntax but I would expect some kind addition accumulation logic – Ninju Bohra Oct 18 '18 at 18:18
  • 1) create another variable initially set to 0 and keep adding the values to it. 2) append the values in a list and use `sum(my_list)` – DavidG Oct 18 '18 at 18:20
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic), [how to ask](http://stackoverflow.com/help/how-to-ask), and [... the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post. – Prune Oct 18 '18 at 18:22
  • Reading a file, converting to integers, and summing a list are all covered quite well on line. Research first, then post. – Prune Oct 18 '18 at 18:23

2 Answers2

0

You can add this to your code.

numberFile = open("random_number.txt", "r" )
sum = 0
for i in numberFile:
    sum += int(i)

print(sum)

At first, sum is 0. The program reads each value and adds the value to sum.

Berdan Akyürek
  • 212
  • 1
  • 14
0

If you want pythonic cryptic code, you can do this in 2 * 2 lines (+1 line of import):

import random

# create file
with open("t.txt","w") as nf:
    nf.write( '\n'.join(map(str,random.choices(range(1,501),
                                               k=int(input("How many numbers?"))))))

The first one uses the inputted value, makes it an int, uses it as "how many" parameter to random.choices() which returns as much random numbers from the given range (1,501) which are then fed into map() to make them strings so join() can make a big string of them wich is then written into the file.

# read / sum file
with open("t.txt","r") as nf:
    print(sum(map(int,(x.strip() for x in nf.readlines() if x.strip()) ) ) )

This reads the whole file as list of lines, strips the newlines, converts them into ints and sums them. (see Doku built-in functions for int, map, sum )


Outputting the generated stuff:

with open("t.txt","r") as nf: 
    print(nf.read())

Sum:

2371

Outputfile:

320
13
138
112
369
339
447
44
211
15
110
253
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69