471,330 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

help - error

4
hi, i m new at scripting, and its very urgent so please help the newbie, i got a this error on a script, and i just cant find where the problem is. here s the script..

def generate_numbers(numbers):
from random import random
f = file(numbers, 'w')
for i in range(100):
f.write('%.2f\n' % (random()* 100))
f.close()

def main():
numbers = 'numbers.txt'
generate_numbers(numbers)

if __name__ == '__main__':
main()
Dec 16 '07 #1
6 1004
bvdet
2,851 Expert Mod 2GB
hi, i m new at scripting, and its very urgent so please help the newbie, i got a this error on a script, and i just cant find where the problem is. here s the script..
Expand|Select|Wrap|Line Numbers
  1. def generate_numbers(numbers):
  2.     from random import random
  3.     f = file(numbers, 'w')
  4.     for i in range(100):
  5.         f.write('%.2f\n' % (random()* 100))
  6.         f.close()
  7.  
  8. def main():
  9.     numbers = 'numbers.txt'
  10.     generate_numbers(numbers)
  11.  
  12. if __name__ == '__main__':
  13.     main()
What error did you receive? I don't see a problem with your code. If I were to write this, the code would probably look something like this:
Expand|Select|Wrap|Line Numbers
  1. from random import random
  2.  
  3. def generate_numbers(numbers):
  4.     f = open(numbers, 'w')
  5.     f.write(''.join(['%.2f\n' % (random()* 100) for _ in range(100)]))
  6.     f.close()
  7.  
  8. if __name__ == '__main__':
  9.     numbers = 'numbers.txt'
  10.     generate_numbers(numbers)
Note the use of code tags. The formatted numbers are compiled into a list with a list comprehension. Instead of writing to the file 100 times, the code writes only once. The numbers are joined into one string using the string method join().
Dec 16 '07 #2
gympy
4
yea your script definetely works, but the thing is that its only a part of the whole script, in whitch i get the "ValueError: I/O operation on closed file"
Anyway, here s the whole script...would be great if you could help me!

Expand|Select|Wrap|Line Numbers
  1. def generiranje_stevil(stevila):
  2.     from random import random
  3.     f = file(stevila, 'w')
  4.     for i in range(100):
  5.         f.write('%.2f\n' % (random()* 100))
  6.         f.close()
  7.  
  8. def preberi_dat(stevila):
  9.     stevila = []
  10.     for line in file(stevila, 'r'):
  11.         stevila.append(float(line.strip()))
  12.     return stevila
  13.  
  14. def povprecje(seznam):
  15.     return sum(seznam)/float(len(seznam))
  16.  
  17. def std_odklon(seznam):
  18.     from math import sqrt
  19.     p = povprecje(seznam)
  20.     return sqrt(sum([(x-p)**2 for x in seznam])/float(len(seznam)))
  21.  
  22. def st_v_intervalu(seznam, min, max):
  23.     seznam.sort()
  24.     stevec = 1
  25.     for x in seznam:
  26.         if x < min: continue
  27.         if x <= max and x >= min: stevec += 1
  28.         if x > max: break
  29.  
  30.     return stevec
  31.  
  32. def glavna_funkcija():
  33.     stevila = 'stevila.txt'
  34.     generiranje_stevil(stevila)
  35.     stevila = preberi_dat(stevila)
  36.     povprecje_stevil = povprecje(stevila)
  37.     stn_odklon_stevil= stn_odklon(stevila)
  38.     print "Stevilo slucajnih stevil, ki so zapisana v datoteki", stevila
  39.     print "v enem standardnem odklonu od povprecja je:",
  40.     print st_v_intervalu(stevila, povprecje_stevil - std_odklon_stevil, povprecje_stevil + std_odklon_stevil)
  41.  
  42.  
  43. if __name__ == '__main__':
  44.     glavna_funkcija():
wot i actually tryed to do, a script that generates 100 random numbers and then export the number in a .txt file, reads the numbers and also calculates : average, std. deviation.....
Dec 16 '07 #3
gympy
4
ok, i went to start from scratch, the first part works, but it doesnt want to read and write the numbers and i m stuck again...any help how to continue

Expand|Select|Wrap|Line Numbers
  1. from random import random
  2.  
  3. def generate_numbers(numbers):
  4.     f = open(numbers, 'w')
  5.     f.write(''.join(['%.2f\n' % (random()* 100) for _ in range(100)]))
  6.     f.close()
  7.  
  8. def read_data(numbers):
  9.     stevila = []
  10.     for line in file("numbers.txt", 'r'):
  11.         stevila.append(float(line.strip()))
  12.     return stevila
  13.  
  14. def average(seznam):
  15.     return sum(seznam)/float(len(seznam))
  16.  
  17. if __name__ == '__main__':
  18.     numbers = 'numbers.txt'
  19.     generate_numbers(numbers)
  20.     numbers = read_data(numbers)
  21.     povprecje_stevil = average(numbers)
Dec 16 '07 #4
bvdet
2,851 Expert Mod 2GB
ok, i went to start from scratch, the first part works, but it doesnt want to read and write the numbers and i m stuck again...any help how to continue

Expand|Select|Wrap|Line Numbers
  1. from random import random
  2.  
  3. def generate_numbers(numbers):
  4.     f = open(numbers, 'w')
  5.     f.write(''.join(['%.2f\n' % (random()* 100) for _ in range(100)]))
  6.     f.close()
  7.  
  8. def read_data(numbers):
  9.     stevila = []
  10.     for line in file("numbers.txt", 'r'):
  11.         stevila.append(float(line.strip()))
  12.     return stevila
  13.  
  14. def average(seznam):
  15.     return sum(seznam)/float(len(seznam))
  16.  
  17. if __name__ == '__main__':
  18.     numbers = 'numbers.txt'
  19.     generate_numbers(numbers)
  20.     numbers = read_data(numbers)
  21.     povprecje_stevil = average(numbers)
I don't see an error, and your code ran OK on my system. What error message did you receive? I reworked function read_data() and changed a few variable names to suit my preferences:
Expand|Select|Wrap|Line Numbers
  1. from random import random
  2.  
  3. def generate_numbers(numbers):
  4.     f = open(numbers, 'w')
  5.     f.write(''.join(['%.2f\n' % (random()* 100) for _ in range(100)]))
  6.     f.close()
  7.  
  8. def read_data(fn):
  9.     return [float(line.strip()) for line in open(fn)]
  10.  
  11. def average(numList):
  12.     return sum(numList)/len(numList)
  13.  
  14. if __name__ == '__main__':
  15.     fn = 'numbers.txt'
  16.     generate_numbers(fn)
  17.     numbers = read_data(fn)
  18.     print average(numbers)
Dec 16 '07 #5
gympy
4
great!!!! thank you so much! everything works fine now....one more question, suppouse i would like the generated numbers to be written out in the idle, how do you do that..oh and i have to add the std_deviation too.
Dec 16 '07 #6
bvdet
2,851 Expert Mod 2GB
great!!!! thank you so much! everything works fine now....one more question, suppouse i would like the generated numbers to be written out in the idle, how do you do that..oh and i have to add the std_deviation too.
This will also print the generated numbers in your IDE:
Expand|Select|Wrap|Line Numbers
  1. from random import random
  2.  
  3. def generate_numbers(numbers):
  4.     f = open(numbers, 'w')
  5.     nums = ''.join(['%.2f\n' % (random()* 100) for _ in range(100)])
  6.     f.write(nums)
  7.     f.close()
  8.     return nums
  9.  
  10. def read_data(fn):
  11.     return [float(line.strip()) for line in open(fn)]
  12.  
  13. def average(numList):
  14.     return sum(numList)/len(numList)
  15.  
  16. if __name__ == '__main__':
  17.     fn = r'H:\TEMP\temsys\numbers.txt'
  18.     print generate_numbers(fn)
  19.     numbers = read_data(fn)
  20.     print average(numbers)
Dec 17 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by wukexin | last post: by
5 posts views Thread by xuatla | last post: by
5 posts views Thread by Marc Violette | last post: by
1 post views Thread by Rahul | last post: by
12 posts views Thread by =?Utf-8?B?ZGdvdw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.