473,241 Members | 2,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 1051
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

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

Similar topics

6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
5
by: Marc Violette | last post by:
<Reply-To: veejunk@sympatico.ca> Hello, I'm hoping someone can help me out here... I'm a beginner ASP.NET developper, and am trying to follow a series of exercises in the book entitled...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
12
by: =?Utf-8?B?ZGdvdw==?= | last post by:
I designed a "contact_us" page in visual web developer 2005 express along with EW2 after viewing tutorials on asp.net's help page. Features work like they should, but I cannot figure out how to...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.