python - need a little help from someone who understand code -
import random def main(): random1 = random.randrange(10,21,1) create_file(random1) process_file() def create_file( random1): generate = open('random_numbers.txt','w') x in range(random1): random2 = random.randrange(1,101,1) generate.write(str(random2)+'/n') generate.close() def process_file(): generate = open('random_numbers.txt','r') entries = 0 total = 0 x in generate: entries += 1 integer = int(x) total = integer + total mean= sum/entries print("there are",(entries),"entries in file.") print("the sum total of entries ",(total)) print("the average of entries ", format((mean),'.2f')) main()
this keeps giving me error , ive don't understand why integer value not working. tried reduce integer=int(x)
integer = x
couldn't use x integer , that's problem in of itself
traceback (most recent call last): file "u:\john oconnor lab 6.2.py", line 41, in <module> main() file "u:\john oconnor lab 6.2.py", line 20, in main process_file() file "u:\john oconnor lab 6.2.py", line 35, in process_file number = int(x) valueerror: invalid literal int() base 10: '29/n59/n17/n2/n8/n14/n2/n14/n9/n21/n5/n25/n15/n47/n'
generate.write(str(random2)+'/n')
are trying write newline @ end there? got escape sequence wrong.
generate.write(str(random2)+'\n')
this ought fix problem in process_file
, since "random_numbers.txt" filled digits separated on own individual lines, rather being digits separated slash , letter n.
also, line:
mean= sum/entries
should use variable total
instead of built-in function sum
.
mean= total/entries
Comments
Post a Comment