如何从python3.6.0中另一个函数中的另一个文件访问函数中修改过的类变量?

2024-10-03 15:32:41 发布

您现在位置:Python中文网/ 问答频道 /正文

正在修改的类:

class Player:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 100
        self.health = self.maxhealth
        self.maxmp = 20
        self.mp = self.maxmp
        self.fireres = 0.1
        self.thunderres = 0.01
        self.totalattack = 15
        self.basedefense = 20
        self.blooddrops = 50
        self.pots = 1
        self.allmagic = ["fireball", "t-phase"]
        self.usemagic = ""
        self.armor = ["dirt rag"]
        self.curarmor = "naked"
        self.weapon = ["snapped sword"]
        self.curweapon = "fist" 
    @property
    def defense(self):
        defense = self.basedefense

        if self.curarmor == "naked":
            defense += 0
            self.fireres += .1
        if self.curarmor == "dirt rag":
            defense += 5
            self.fireres += .1
        if self.curarmor == "leather armor":
            defense += 10

        if self.curarmor == "metal armor":
            defense += 20

        return defense

    @property
    def attack(self):
        attack = self.totalattack

        if self.curweapon == "snapped sword":
            attack += 5

        if self.curweapon == "greatsword":
            attack += 15

        if self.curweapon == "sword":
            attack += 7

        if self.curweapon == "fist":
            attack += 0

        return attack

文件1:

import sys
import os
import time
import random
from File1 import name


name()
  1. 文件1转到文件2

文件2:

import sys
import os
import time
import random
from abc import ABCMeta, abstractmethod
from File3 import start1
from Classes_Player import Player

def name():
    clear = lambda: os.system('cls')
    print("Have you got a name lad!?")
    option2 = input("Type (Yes or No):").lower()
    if option2 == "yes" or option2 == "y":
        print("Ah, brilliant!")
        time.sleep(1)
        print("What be it?")
        option = input("Type:")
        global PlayerIG
        PlayerIG = Player(option)
        if len(option) >= 20:
            print("shorten it a bit please, for brevity")
            time.sleep(2)
            clear()
            name()
        elif len(option) == 0:
            print("Lad that's not a name, that's emptiness")
            time.sleep(2)
            clear()
            name()
        else:
            print("Are you sure? (Yes or No)")
            ans = input().lower()
            if ans == "yes" or ans == "y":
               print("Alrighty then, just wanted to be sure lad.")
               time.sleep(2)
               start1()
  1. 它在这里所做的是修改Player类,并使其成为类的name变量是用户的输入(我相信)。然后转到文件3

文件3

import sys
import os
import time
import random
from abc import ABCMeta, abstractmethod
from Intro import *


def start1():
    clear = lambda: os.system('cls')
    clear()
    print("Name: {}".format(PlayerIG.name))
    print("Health: {}/{}".format(PlayerIG.health, PlayerIG.maxhealth))
    print("Attack: {}".format(PlayerIG.attack))
    print("Defense: {}".format(PlayerIG.defense))
    print("Mana: {}/{}".format(PlayerIG.mp, PlayerIG.maxmp))
    print("Equipped Armor: {}".format(PlayerIG.curarmor))
    print("Equipped Weapon: {}".format(PlayerIG.curweapon))
    print("Healing potions: {}".format(PlayerIG.pots))
    print("Wealth: {}\n".format(PlayerIG.blooddrops))
    print("1.) Inventory")
    print("2.) Check Player")
    print("3.) Save")
    print("4.) Fight")
    print("5.) Shop")
    print("6.) Exit")
    ans = input().lower()
    if ans == "inventory" or ans == "i" or ans == "1":
        inventory()
    elif ans == "check player" or ans == "cp" or ans == "check" or ans == "c" or ans == "2":
        status()
    elif ans == "save" or ans == "s" or ans == "3":
        clear()
    elif ans == "exit" or ans == "e" or ans == "6":
        exit
    elif ans == "fight" or ans == "f" or ans == "4":
        prefight()
    elif ans == "shop" or ans == "s" or ans == "5":
        shop_keeper()
    else:
        start1()
  1. 我在这里得到了一个错误"NameError: name 'PlayerIG' is not defined",我已经查过了,但是对于这个实例没有任何帮助。感谢您的帮助

Tags: ornameimportselfformatiftimeplayer