Creating a save function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xZaft
    New Member
    • Jul 2007
    • 7

    #1

    Creating a save function

    Code:
    def save():
        try:
            saves.append(str(level))
            saves.append(str(stre))
            saves.append(str(spd))
            saves.append(str(hp))
            saves.append(str(maxhp))
            saves.append(str(exp))
            saves.append(str(maxexp))
            saves.append(str(gp))
            saves.append(str(sword))
            save = open('gamesave.txt', 'r+')
            for s in saves:
                save.write(s)
                save.write('\n')
                saves.remove(s)
                print s
            print "Save successful."
            save.close()
        except ValueError: print "Save unsuccessful."
    This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.

    The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.

    Anyone know why it won't get all the way through the list?

    [link removed]
    Last edited by bartonc; Jul 12 '07, 05:16 PM. Reason: removed link
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by xZaft
    Code:
    def save():
        try:
            saves.append(str(level))
            saves.append(str(stre))
            saves.append(str(spd))
            saves.append(str(hp))
            saves.append(str(maxhp))
            saves.append(str(exp))
            saves.append(str(maxexp))
            saves.append(str(gp))
            saves.append(str(sword))
            save = open('gamesave.txt', 'r+')
            for s in saves:
                save.write(s)
                save.write('\n')
                saves.remove(s)
                print s
            print "Save successful."
            save.close()
        except ValueError: print "Save unsuccessful."
    This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.

    The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.

    Anyone know why it won't get all the way through the list?
    During the iteration, items are being removed from the list. Do something like this instead:[code=Python]>>> myList = [1,2,3,4,5,6,7,8 ,9,0]
    >>> for item in myList:
    ... print item
    ... myList.remove(i tem)
    ...
    1
    3
    5
    7
    9
    >>> myList
    [2, 4, 6, 8, 0]
    >>>
    >>> myList = [1,2,3,4,5,6,7,8 ,9,0]
    >>> for item in myList[:]:
    ... print item
    ... myList.remove(i tem)
    ...
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    >>> myList
    [][/code]

    Comment

    • xZaft
      New Member
      • Jul 2007
      • 7

      #3
      Originally posted by bvdet
      During the iteration, items are being removed from the list. Do something like this instead:[code=Python]>>> myList = [1,2,3,4,5,6,7,8 ,9,0]
      >>> for item in myList:
      ... print item
      ... myList.remove(i tem)
      ...
      1
      3
      5
      7
      9
      >>> myList
      [2, 4, 6, 8, 0]
      >>>
      >>> myList = [1,2,3,4,5,6,7,8 ,9,0]
      >>> for item in myList[:]:
      ... print item
      ... myList.remove(i tem)
      ...
      1
      2
      3
      4
      5
      6
      7
      8
      9
      0
      >>> myList
      [][/code]
      Thanks that worked very well =]

      I have another concern though. I have the game set up in functions so that the switch is easier to read.

      [code=Python]
      while c != 'q':
      main()
      c = raw_input("[A]ttack [S]tore S[t]ats [L]oad Sa[v]e [Q]uit ").lower()
      clear()
      if c == 'a':
      attack()
      elif c == 's':
      store()
      elif c == 't':
      stats()
      elif c == 'l':
      load()
      elif c == 'v':
      save()
      else:
      if c == 'q':
      gb = raw_input("Good bye! Press [Enter]")
      else:
      print "Invalid Choice."
      [/code]

      main has all of the basic variables already defined for later use. When going to attack I get the error:
      Code:
      Traceback (most recent call last):
        File "C:\Python25\Tools\custom\game.py", line 210, in <module>
          attack()
        File "C:\Python25\Tools\custom\game.py", line 52, in attack
          if hp > 0:
      UnboundLocalError: local variable 'hp' referenced before assignment
      but all the code up to that point in attack was already defined:

      [code=Python]
      def attack():
      monName = _choice(monster Name)
      print "You are attacking a %s" % monName
      monIndex = monsterName.ind ex(monName)
      monHP = monsterHP[monIndex]
      monExp = monsterExp[monIndex]
      monStr = monsterStr[monIndex]
      monSpd = monsterSpd[monIndex]
      monMin = monsterMin[monIndex]
      monMax = monsterMax[monIndex]

      if sword == 1:
      stre += 4
      spd += 3
      if hp > 0:
      [/code]

      Why does that error come up?

      Comment

      • elbin
        New Member
        • Jul 2007
        • 27

        #4
        Originally posted by xZaft
        main has all of the basic variables already defined for later use. When going to attack I get the error:
        Code:
        Traceback (most recent call last):
          File "C:\Python25\Tools\custom\game.py", line 210, in <module>
            attack()
          File "C:\Python25\Tools\custom\game.py", line 52, in attack
            if hp > 0:
        UnboundLocalError: local variable 'hp' referenced before assignment
        but all the code up to that point in attack was already defined:

        [code=Python]
        def attack():
        monName = _choice(monster Name)
        print "You are attacking a %s" % monName
        monIndex = monsterName.ind ex(monName)
        monHP = monsterHP[monIndex]
        monExp = monsterExp[monIndex]
        monStr = monsterStr[monIndex]
        monSpd = monsterSpd[monIndex]
        monMin = monsterMin[monIndex]
        monMax = monsterMax[monIndex]

        if sword == 1:
        stre += 4
        spd += 3
        if hp > 0:
        [/code]

        Why does that error come up?
        Hp is NOT defined for your attack function, that is, it is not defined in it OR as a global variable in the file/module. So you get an error. If it IS defined globally, try:

        Code:
        if global hp > 0:

        Comment

        • xZaft
          New Member
          • Jul 2007
          • 7

          #5
          [code=Python]
          if global hp > 0:
          [/code]
          When I try and run that, it says it is invalid syntax.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by xZaft
            [code=Python]
            if global hp > 0:
            [/code]
            When I try and run that, it says it is invalid syntax.
            You'll need to post more of your program in order to get the scope of your variables worked out. Please start a new thread regarding "creating an RPG" or something like that.

            Thanks.

            Comment

            Working...