Python Forum
Adding values to list and pickling
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding values to list and pickling
#1
Hey everyone!
I have (for me) a big problem! I'm trying to repair it for few days and I have just run out of power and ideas what to do with it. My problem is that:
I have a pickled list, that already have some values inside.
Then I want to exchange those values with new ones by function and by adding things out of the function (the last one: it writes down the change when I print it, but... it doesn't save it in a file)
PLS HELP!
import pickle
import random
from tryb_walki import fight
from wczytywanie_zapisków import loading
from add_points import add_points
print('1. Nowa gra')
print('2. Wczytaj grę')
print('3. Wyjdź z gry')
menu_choice = 0
new_game=1
load_game=2
end_game=3
minimum_menu_choice=1
maximum_menu_choice=3
while not (menu_choice >= minimum_menu_choice and menu_choice <= maximum_menu_choice):
  try:
    menu_choice = int(input('Wpisz numer zadania które chciałbyś wykonać:'))
  except:
    print('Wpisz numere od 1 do 3')


if menu_choice == new_game:

  print('''
  #..#.###..####.#####.#####.####.###.
  #.#..#.#..##...#...#...#...#..#.#.#.
  ##...###..####.#####...#...#..#.###.
  #.#..#.#..#....#...#...#...#..#.#.#.
  #..#.#..#.####.#...#...#...####.#..#
  ....................................
  ###.####.####.#####.#####.####.#....
  #.#.#..#.#......#...#...#.#....#....
  ###.#..#.####...#...#####.#....#....
  #...#..#....#...#...#...#.#....#....
  #...####.####...#...#...#.####.#....''')

  min_choice_race=1
  max_choice_race=4
  save = open('game_save', 'wb')
  name = input('Wpisz swój nick')
  choice_race = 0
  while not (choice_race >= min_choice_race and choice_race <= max_choice_race):
    while True:
      try:
        choice_race = int(input('''Wpisz jedną z 4 cyferek- każda oznacza inną rasę:
      1-Wojownik                        2-Łucznik                      3-Mag                        4-Ninja
      - Posiada tarcze (10% szans         - atakuje z dystancu (3       - Może się leczyć (20%       - Jest bardzo zwinny (20%
        na obronę przed atakiem)            pierwsze ciosy w walce        szans na przywrócenie        na uniknięcie ciosu)
                                            są jego)                      punktów życia z zakresu    - pierwszy cios jest
                                                                            10 co atak)                zawsze jego
      - 150 punktow życia                 - 80 punktów życia            - 50 punktów życia           - 120 punktów życia  
      - zadaje obrażenia z zakresu        - zadaje obrażenia z zakresu  - Zadaje obrażenia z zakresu - Zadaje obrażenia z                           
        8-10                                12-15                         15-20                        zakresu 10-12     '''))

      except:
        print("Wpisz liczbę z zakresu od 1 do 4, potwierdź enterem")
      break
  warrior=1
  archer=2
  wizard=3
  if choice_race == warrior:
    race = [name, 'wojownik', 150, [8, 10], 10, 0, 0, 1]#nazwa,klasa, hp, obrażenia, szansa na unik, szansa na leczenie, leczone punkty, poziom
  elif choice_race == archer:
    race = [name, 'łucznik', 80, [12, 15], 0, 0, 0, 1]
  elif choice_race == wizard:
    race = [name, 'mag', 50, [15, 20], 0, 20, 10, 1]
  else:
    race = [name, 'ninja', 120, [10, 12], 20, 0, 0, 1]
  pickle.dump(race, save)
  save.close()
  save= open ('game_save', 'rb')
  all_saves = pickle.load(save)
  
  input('Wciśnij enter, jeżeli jesteś gotowy na pierwszą misję')




elif menu_choice == load_game:
  
  save = open('game_save', 'rb')
  all_saves = pickle.load(save)

  


  print (loading(all_saves))
  input('enter')



if all_saves [7] == 1 :
  input ('Witaj na pierwszej misji, aby przejść dalej wciśnij (enter)')
  input('Pracujesz w służbie u króla, a ta misja ma za zadanie przeszkolić Cię do prawdziwej walki, (enter)')
  input ('Pierwsze wyzwanie: (enter)')
  input('Pokonaj trenera(enter)')
  trener=['Trener', 'wojownik', 20, [2,5],30, 0, 0]


  print(fight(all_saves, trener))
  
  save.close()

  if trener[2]<=0:
    save = open('game_save', 'r+b')
    all_saves = pickle.load (save)
    all_saves[7]+=1
    input('Okej- szkolenie zakończone')
    print('Otrzymujesz 5 punktów do rozdania do swoich umiejętności. Co chciałbyś ulepszyć?')
    print(add_points(5, all_saves))
    print(all_saves)
    save.close()

#add_points

def add_points(amount_points,character):
  print('Otrzymujesz', amount_points ,'punktów do rozdania do swoich umiejętności. Co chciałbyś ulepszyć?')
  minimum_number=1
  maximum_number=5
  number=0
  any_points=0 
  hp = int (character[2])
  minimal_hit = int (character[3][0])
  maximal_hit = int (character[3][1])
  miss_chance = int (character[4])
  heal_chance =int (character[5])
  amount_heal = int (character[6])


  while amount_points>any_points:
    print('masz', amount_points, 'punktów') 
    print('Jak narazie twoje umiejętności wynoszą: ')
    print('1. Punkty życia:', hp)
    print('2. Zadawane obrażenia:', minimal_hit, '-', maximal_hit)
    print('3. Szansa na unik:', miss_chance)
    print('4. Szansa na uleczenie:', heal_chance)
    print('5. Leczone punkty życia:', amount_heal)
    
    
    while not (number>=minimum_number and number<=maximum_number):
      
      while True :
        
        try:
          number=int(input('Wybierz numer rzeczy którą chciałbyś wzmocnić:'))
          print(character [number+1])
          break
        except IndexError:
          print ('wpisałeś złą liczbę')
        except ValueError:
          print('wpisz coś', 'albo przestań wpisywać tekst')
        except:
          print("znowu coś jest nie tak")
    
    
    while True:
      try:
        how_many=int(input('O ile?'))
        break
      except IndexError:
        print ('Wpisałeś złą liczbę')
      except ValueError:
        print('Wpisz coś', 'albo przestań wpisywać tekst')
      except:
        print("znowu coś jest nie tak")

    number_of_ability=number+1
    if how_many<=amount_points:
      amount_points-=how_many
      
      if number_of_ability==2:
        hp+=how_many

      
      elif  number_of_ability==3:
        minimal_hit+=how_many
        maximal_hit+=how_many
        
      
      elif number_of_ability==4:
        miss_chance+=how_many
      
      elif number_of_ability==5:
        heal_chance+=how_many

      elif number_of_ability==6:
        amount_heal+=how_many
    

    else:
      print ('masz za mało punktów')
  
  
  print ('nie masz już więcej punktów')
  print ('twoje umiejętności, to:')
  print('Punkty życia:', hp)
  print('Zadawane obrażenia:', minimal_hit, '-', maximal_hit)
  print('Szansa na unik:', miss_chance)
  print('Szansa na leczenie:', heal_chance)
  print('Leczone punkty życia:', amount_heal)
  
Reply
#2
Hi, we are glad to help. But you will need to provide code you use and errors you get (if you get them) - full traceback message. In case the outcome is not as you expect, write the actual vs. desired output. And make sure to use code tags, you can find help here.
Reply
#3
https://repl.it/@mefiak/Gra-2
Ok, so here is the full code.
What I want is to get rewrote values in list (all_saves) from function (add_points) and from all_saves[7] += 1.
Then I want to get them saved in game_save.
Sorry, but idk how to tag this post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,015 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,063 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  adding parts of a list Eric7Giants 4 2,731 Nov-17-2019, 05:53 PM
Last Post: buran
  dictionaries and list as values Pippi 6 3,464 Apr-13-2019, 09:05 AM
Last Post: perfringo
  Turtle Graphics Card Values in a List Muzz 0 2,341 Apr-11-2019, 12:55 PM
Last Post: Muzz
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,585 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,762 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  Storing Minimum List of values from a recursive function sigsegv22 1 2,528 Sep-10-2018, 01:25 PM
Last Post: ichabod801
  "List index out of range" for output values pegn305 3 5,292 Nov-26-2017, 02:20 PM
Last Post: heiner55
  adding a number to the list atux_null 4 3,845 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020