Passing varyables through a text file

Reply

Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Passing varyables through a text file

 
0
  #1
Nov 14th, 2008
I'm making a text game and im making a save() and load() function i'v got the save function to work but i dont know how to change varyables in the program depending on the varyables in the text files.
heres the code for the functions:
  1. def save():
  2. openfile = open('save_file', 'r+')
  3. openfile.seek(0, 0)
  4. openfile.write(player.exp, '/n', player.level, '/n', player.health, '/n', new_monster.health, '/n', new_monster, '/n', new_monster.name, )
  5.  
  6. def load():
  7. openfile = open('save_file', 'r')
  8. openfile.read << this is where im stuck

any help would be apreciated
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Passing varyables through a text file

 
0
  #2
Nov 14th, 2008
And i just found out that the save() function doent work either, any ideas?
...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Passing varyables through a text file

 
0
  #3
Nov 14th, 2008
To load the values, you could do something like this:
Example config file:
Tom
10
9.7
[1,2,3]
  1. fn = 'config.txt'
  2. f = open(fn)
  3. vartypes = [str,int,float,eval]
  4. varnames = ['name','exp','score','other']
  5. dd = {}
  6. for i,line in enumerate(f):
  7. dd[varnames[i]] = vartypes[i](line.strip())
  8.  
  9. globals().update(dd)
  10. f.close()
  1. >>> name
  2. 'Tom'
  3. >>> other
  4. [1, 2, 3]
  5. >>>
To write the variables to disk, you must pass a string argument to the file write() method.
  1. f = open('config1.txt', 'w')
  2. f.write('\n'.join([str(item) for item in [name,exp,score,other]]))
  3. f.close()
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 899
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Passing varyables through a text file

 
1
  #4
Nov 14th, 2008
you can also use eval or exec on the text file if it was like this:
t = 1
variable2 = 10
string = 'hello'
if you had this in your program
  1. f = open('variables.txt')
  2. for line in f:
  3. eval(line)
  4. #or exec(line)
That will then set up all of your variables.
Hope that helps
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Passing varyables through a text file

 
0
  #5
Nov 18th, 2008
Nah that doesnt work, heres the code as it is now:
  1. #the save function
  2. def save():
  3. save_varyables = [player.level, player.health, new_monster.health, new_monster, new_monster.name]
  4. file = open('save_varyables')
  5. pickle.dump(save_varyables, file)
  6. file.close()
  7.  
  8. #the load function
  9. def load():
  10. f = open('save_file')
  11. for line in f:
  12. eval(line)
...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Passing varyables through a text file

 
0
  #6
Nov 18th, 2008
Well, of course it won't work. If you pickle.dump() some data to disk, you need to pickle.load() to get the data from disk. Example:
  1. >>> dd = {'s3': 'This is a string', 'r3': 1.5, 'i3': 1}
  2. >>> f = open('test.txt', 'w')
  3. >>> import pickle
  4. >>> pickle.dump(dd, f)
  5. >>> f.close()
  6. >>> f = open('test.txt')
  7. >>> dd1 = pickle.load(f)
  8. >>> dd1
  9. {'s3': 'This is a string', 'i3': 1, 'r3': 1.5}
  10. >>>
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Passing varyables through a text file

 
0
  #7
Nov 20th, 2008
I think that will work but i cant tell unless the save function works, i dont know why it works, i tell it to ask the user if he\she wants to save and when they type yes, it dosnt work because when it saves it is supposed to quit and i went and chacked the text file that it is supposed to save in and it hasnt dumped anything there, any ideas?
here is the code yet again:
  1. #the save function
  2. def save():
  3. save_game_varyables = [new_monster.attack, new_monster.health, new_monster.defense, new_monster,name]
  4. file = open('save_file')
  5. pickle.dump(save_game_varyables, file)
  6. file.close()
  7. loop = 0
  8.  
  9. #the load function
  10. def load():
  11. f = open('save_file')
  12. pickle.load(f)
  13. for line in f:
  14. eval(line)
...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 899
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Passing varyables through a text file

 
0
  #8
Nov 20th, 2008
For starters you shouldnt use the word "file" as a variable. That is a keyword in python.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Passing varyables through a text file

 
0
  #9
Nov 21st, 2008
You opened the file in mode 'r'. Try mode 'w'.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Passing varyables through a text file

 
0
  #10
Nov 21st, 2008
I think the error is how call the class in the main() function can anyone help?
heres the code:
  1. #!/usr/bin/env python
  2. #The main file with all the game logic and the player class
  3.  
  4. #import the necessary files
  5. import random
  6. import pickle
  7. from monsters import *
  8. from weapons import *
  9.  
  10. try: import cPickle as pickle
  11. except: import pickle as pickle
  12.  
  13. class Packet:
  14. def __init__(self, data):
  15. self.data = data
  16.  
  17. def save(self):
  18. #save
  19. data = pickle.dumps(Packet(data))
  20. savefile = open('save_file','w')
  21. savefile.write(data)
  22. savefile.close()
  23.  
  24. def load(self):
  25. #load
  26. data = pickle.loads(buf).data
  27. openfile = open('save_file','r')
  28. data = openfile.read()
  29. if IOError:
  30. print 'No save file to load!'
  31. loop = 0
  32.  
  33.  
  34. #Player class
  35. class player():
  36. def __init__(self):
  37. self.health = 30
  38. self.damage = 5
  39. weapon = random.randrange(1,3)
  40. if weapon == 1:
  41. self.weapon = sword()
  42. print 'The weapon that you are using\'s attributes are:'
  43. print self.weapon.GetAttributesInfo()
  44. elif weapon == 2:
  45. self.weapon = axe()
  46. print 'The weapon that you are using\'s attributes are:'
  47. print self.weapon.GetAttributesInfo()
  48. elif weapon == 3:
  49. self.weapon = spear()
  50. print 'The weapon that you are using\'s attributes are:'
  51. print self.weapon.GetAttributesInfo
  52.  
  53. def attack(self, monster):
  54. dam = self.weapon.GetDamage()
  55. if dam > monster.defense:
  56. monster.health = monster.health - dam
  57. print 'You hit him for %d damage!' %dam
  58. else:
  59. print 'You did\'nt do any damage...'
  60.  
  61. #The main() function, for executing the actuall game
  62. def main():
  63. loop = 1
  64. new_monster = random.randrange(1, 5)
  65. if new_monster == 1:
  66. new_monster = orc()
  67. elif new_monster == 2:
  68. new_monster = troll()
  69. elif new_monster == 3:
  70. new_monster = dragon()
  71. elif new_monster == 4:
  72. new_monster = goblin()
  73. elif new_monster == 5:
  74. new_monster = spider()
  75. saving_and_loading = Packet('save_file')
  76. P = player()
  77. start = 1
  78. while start == 1:
  79. print 'Your options are:'
  80. print '1)New game'
  81. print '2)Load'
  82. print '3)Quit'
  83. print 'You can type \'save\' at any time during the game to save your game(saving quits)'
  84. choice = input('What do you want to do:')
  85. if choice == 1:
  86. print 'The attacking monster\'s attributes are:'
  87. print new_monster.GetAttributesInfo()
  88. P.attack(new_monster)
  89. elif choice == 2:
  90. Packet.load()
  91. print 'The attacking monster\'s attributes are:'
  92. print new_monster.GetAttributesInfo()
  93. P.attack(new_monster)
  94. elif choice == 3:
  95. loop = 0
  96. start = 0
  97. loop = 1
  98. while loop == 1:
  99. #The game logic connected to all the varyables
  100. if new_monster.health <= 0:
  101. print 'You killed the ', new_monster.name, '!'
  102. loop = 0
  103. elif new_monster.health > 0:
  104. choice = raw_input('Do you want to attack again:')
  105. if choice == 'yes' or 'y':
  106. print 'The monster\'s health is now:', new_monster.health
  107. P.attack(new_monster)
  108. elif choice == 'no':
  109. loop = 0
  110. elif choice == 'save':
  111. Packet.save()
  112. else:
  113. print 'You\'re just talking jiberish now'
  114.  
  115. #Initalize the main function, since __name__ is always == '__main__'
  116. if __name__ == '__main__':
  117. main()
...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC