Python Pickle.dump 适用于在 while 循环中声明的变量,但不适用于在几乎完全相同的 while 循环中声明的变量?

问题描述 投票:0回答:1

所以我和我的朋友决定制作一个基于文本的角色扮演游戏,以刷新我们的Python技能。好吧,我们已经进入了角色类别选择屏幕,并使用 python pickle 模块来保存数据。我们使用 while true 循环和 readchar readkey() 函数来检测用户输入。当玩家选择一个职业并确认它时,循环应该被打破,然后数据应该被保存。然而,尽管其他两个类使用完全相同的 while 循环公式,但有一些细微的差异,但仅第一个类的数据被成功保存。这是任何想要运行它的人的完整代码。我很抱歉这个可怕的标题,因为我什至不知道如何在标题中描述它。

import random
import pickle
import colorama
from colorama import Fore, Back, Style
import readchar
from readchar import readkey, key
import time
from time import sleep
import os



# tutorial
print("New to the game? If so, press Y. If not, press N. \n")
while True:
  k = readkey()
  if k == "y":
    print('Welcome to RPyG. This game is a text based RPG with multiple mechanics you need to famaliarize yourself with.\n')
    print("Dexterity is the measure of how good a character can dodge. The higher it is, the higher the chance they'll be able to dodge your attack.\n")
    print("In order to counter this, power attacks exist. They take two turns minimum to be able to use, and gain 1 extra damage for each round charged. However, power attacks can be interrupted by a interrupt attack that always has a chance to be dodged, no matter how slow you are. \n")
    print("\n Health is how many times a character can take a hit. If you get successfully attacked, you will lose health. \n")
    print("When you are done reading, press K.")
    while True:
      k = readkey()
      if k == "k":
        break
    break
  if k == "n":
    break
os.system('clear')
#choose class
a = Fore.RED + "Knight"
b = Fore.BLUE + "Mage"
c = Fore.GREEN + "Archer"
print("ADD A THING TO LOAD A SAVEFILE OR START NEW HERE. OK THIS IS JUST HERE SO ITS NOT FORGOTTEN AND ADDED AFTER 5000 YEARS")
print("Before we begin, choose a class. \n")
print(Fore.CYAN + Style.BRIGHT + "1: " + Fore.WHITE + Style.NORMAL + "[" + a + Fore.WHITE + "]" + Fore.LIGHTBLACK_EX + "- Sword and Shield - Can block attacks and use potions - Dexterity: Medium - Health: High")
print(Fore.CYAN + Style.BRIGHT + "2: " + Fore.WHITE + Style.NORMAL + "[" + b  + Fore.WHITE + "]" + Fore.LIGHTBLACK_EX + "- Wooden Staff - Can use Spells - Dexterity: Slow - Health: Medium")
print(Fore.CYAN + Style.BRIGHT + "3: " + Fore.WHITE + Style.NORMAL + "[" + c + Fore.WHITE + "]" + Fore.LIGHTBLACK_EX + "- Short bow and Dagger- Can shoot arrows - Dexterity: High - Health: Low")
print(Fore.WHITE + Style.NORMAL)
# saving data
while True:
  k = readkey()
  if k == "1":
    print("You have chosen the " "[" + a + Fore.WHITE + "]" "! This decision cannot be changed unless you start a new game. To go back, press N. To continue, press Y.")
    while True:
      k = readkey()
      if k == "n":
        print("Game closing. Run again to choose another character.")
        sleep(3)
        exit()
      if k == "y":
        print("Class chosen! \n")
        save_data = {"class":1, "health":5, "strength":4, "dexterity":3, "gold":0 }
        break
  break
# MAGE choice
  if k == "2":
    print("You have chosen the " "[" + b + Fore.WHITE + "]" "! This decision cannot be changed unless you start a new game. To go back press N. To continue, press Y.")
    while True:
      k = readkey()
      if k == "n":
        print("Game closing. Run again to choose another character.")
        sleep(3)
        exit()
      if k == "y":
        print("Class chosen! \n")
        save_data = {"class":2, "health":4, "magic":4, "dexterity":3, "gold":0 }
        break
  break 
# Archer choice
  if k == "3":
    print("You have chosen the " "[" + c + Fore.WHITE + "]" "! This decision cannot be changed, unless you start a new game. To go back press N. to continue, press Y ")
    while True:
      k = readkey()
      if k == "n":
        print("Game closing. Run again to choose another character.")
        sleep(3)
        exit()
      if k == "y":
        print("Class chosen! \n")
        save_data = {"class":3, "health":3, "dexterity":6, "strength":3, "gold":0 }
        break
  break

save_name_ask = input("Please enter a name for the game's savefile. \n")

save_game = open(save_name_ask,"wb")
pickle.dump(save_data, save_game)
save_game.close()

load_game = open(save_name_ask,"rb")
loaded_data = pickle.load(load_game)
print(loaded_data)

我们尝试在每个 while 循环中将所有保存数据名称命名为不同的名称,但它没有弹出 save_name_ask 输入,我们尝试多次重新运行它,但第一个类是唯一有效的。其他两个类产生错误:

Traceback (most recent call last):
  File "main.py", line 89, in <module>
    pickle.dump(save_data, save_game)
NameError: name 'save_data' is not defined. Did you mean: 'save_game'?

感谢任何帮助,但是我什至不知道是否有解决方案来解决这个令人厌恶的意大利面条代码,这会让 Guido van Rossum 失望地哭泣。

python python-3.x while-loop pickle break
1个回答
-1
投票

你尝试过将其关闭然后重新打开吗?/j

© www.soinside.com 2019 - 2024. All rights reserved.