Question

i'm trying to load a binary file with pickle that has a record in a list, like so

import pickle
class player_energy_usage():
def __init__(self):
    self.weapons = 25
    elf.shields = 25
    self.life_support = 25
    self.engines = 25

def p_eu_init():
    global p_energy   
    p_energy = []
    player_ship_energy = player_energy_usage()
    p_energy.append(player_ship_energy)
    pickle.dump(p_energy,open('p_energy.dat','wb'))

p_eu_init()
pickle.load('rb'('p_energy.dat'))
print('Weapons are using {0}% of energy'.format(p_energy[0].weapons))
print('Shields are using {0}% of energy'.format(p_energy[0].shields))
print('Life Support is using {0}% of energy'.format(p_energy[0].life_support))
print('Engines is using {0}% of energy'.format(p_energy[0].engines))

However i get a type error,

Traceback (most recent call last):
File "E:/Python texted based game/Tests/file loading test.py", line 18, in <module>
pickle.load('rb'('p_energy.dat'))
TypeError: 'str' object is not callable

thanks for the help.

Était-ce utile?

La solution

That is not the correct syntax. It should be instead:

p_energy = pickle.load(open('p_energy.dat', 'rb'))

What you're actually doing is:

'rb'('p_energy.dat') is trying to call the str object 'rb' with an argument of 'p_energy.dat', which is why you get the error 'str' object is not callable.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top