Home > Mobile >  How to save a datafile in python
How to save a datafile in python

Time:02-11

They let me at school make a program at school to roll a die 100 times, save it to a file and then graph the results. I already made the program but I can't save it, I tried this but I can't see the file, could someone help me please? This is my code:

import random
import pickle

def dado(n):
  result = []
  for i in range(0,n):
    result.append(random.randint(1, 6))
  return result
print(dado(100))

#save the list
pickle.dump(dado, open("dado.txt", "wb"))

CodePudding user response:

this save program may like this

import random
import pickle

def dado(n):
    result = []
    for i in range(0,n):
        result.append(random.randint(1, 6))
    return result
    print(dado(100))

#save the list
pickle.dump(dado(100), open("dado.pkl", "wb"))

or

import random
import numpy as np

def dado(n):
    result = []
    for i in range(0,n):
        result.append(random.randint(1, 6))
    return result
    print(dado(100))

#save the list
np.set_printoptions(suppress=True)
np.savetxt("dado.txt", dado(100), fmt='%.1f')
  • Related