Python Forum
So Many Errors... No Way To Solve
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
So Many Errors... No Way To Solve
#1
Hi there. New to the forum here. Taking a class on Python, using 3.6. I think many of the problems I keep running into in this class are that this instructor is using an older version than I am, but that's just a theory. Please remember I'm a complete noob when it comes to programming, and I really need someone to explain to me like I'm 5 about these concepts, even though this course is advertised as being for beginners.
Anyway, here are my problems that I could really use some explaining.

In the class, we're constructing a simple RPG with attacks and magic. It's already been sectioned into two different pieces.
Here's the first part (Game.py):
import random


class bcolors:
    HEADER = '\033[995m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m]'
    UNDERLINE = '\033[4m'


class Person:
    def __init__(self, hp, mp, atk, df, magic):
        self.maxhp = hp
        self.hp = hp
        self.maxmp = mp
        self.mp = mp
        self.atkl = atk - 10
        self.atkh = atk + 10
        self.df = df
        self.magic = magic
        self.actions = ["Attack", "Magic"]

    def generate_damage(self):
        return random.randrange(self.atkl, self.atkh)

    def generate_spell_damage(self, i):
        mgl = self.magic[i]["dmg"] - 5
        mgh = self.magic[i]["dmg"] + 5
        return random.randrange(mgl, mgh)

    def take_damage(self, dmg):
        self.hp -= dmg
        if self.hp < 0:
            self.hp = 0
        return self.hp

    def get_hp(self) -> object:
        return self.hp

    def get_max_hp(self):
        return self.maxhp

    def get_mp(self):
        return self.mp

    def get_max_mp(self):
        return self.maxmp

    def reduce_mp(self, cost):
        self.mp -= cost

    def get_spell_name(self, i):
        return self.magic[i]["name"]

    def get_spell_mp(self, i):
        return self.magic[i]["cost"]


    def choose_action(self):
        i = 1
        print("Actions")
        for item in self.actions:
            print(str(i) + ":", item)
            i += 1

    def choose_magic(self):
        i = 1
        for spell in self.magic:
            print("Magic")
            print(str(i) + ":", spell["name"], "(cost:", str(spell["mp"]) + ")")
            i += 1


Here's the second part (main.py):
from Classes.Game import Person, bcolors

magic = [{"name": "Fire", "cost": 10, "dmg": 100},
         {"name": "Thunder", "cost": 10, "dmg": 124},
         {"name": "Blizzard", "cost": 10, "dmg": 100}]


player = Person(460, 65, 60, 34, magic)
enemy = Person(1200, 65, 45, 25, magic)

running = True
i = 0

print(bcolors.FAIL + bcolors.BOLD + "An enemy attacks!" + bcolors.ENDC)

while running:
    print("==================")
    player.choose_action()
    choice = input("Choose action:")
    index = int(choice) - 1

    if index == 0:
        dmg = player.generate_damage()
        enemy.take_damage(dmg)
        print("You attacked for", dmg, "Points of damage.")
    elif index == 1:
        player.choose_magic()
        magic_choice = int(input("Choose Magic:")) - 1
        magic_dmg = player.generate_spell_damage(magic_choice)
        spell = player.get_spell_name(magic_choice)
        cost = player.get_spell_mp_cost(magic_choice)

        current_mp = player.get_mp()

        if cost > current_mp:
            print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC)
            continue

    player.reduce_mp(cost)
    enemy.take_damage(magic_dmg)
    print(bcolors.OKBLUE + "\n" + spell + " deals", str(magic_dmg), "points of damage" + bcolors.ENDC)

    enemy_choice = 1

    enemy_dmg = enemy.generate_damage()
    player.take_damage(enemy_dmg)
    print("Enemy attacks for", enemy_dmg)

    print("-----------------------------------------")
    print("Enemy HP:", bcolors.FAIL + str(enemy.get_hp) + "/" + str(enemy.get_max_hp()) + bcolors.ENDC + "/n")

    print("Your HP:", bcolors.OKGREEN + str(player.get_hp()) + "/" + str(player.get_max_hp()) + bcolors.ENDC)
    print("Your MP:", bcolors.OKBLUE + str(player.get_mp()) + "/" + str(player.get_max_mp()) + bcolors.ENDC + "\n")

    if enemy.get_hp() == 0:
        print(bcolors.OKGREEN + "You Win!" + bcolors.ENDC)
        running = False
    elif player.get_hp() == 0:
        print(bcolors.FAIL + "Your enemy had defeated you!" + bcolors.ENDC)
        running = False
And here's the prompt that I got so far:
Output:
]An enemy attacks! ================== Actions 1: Attack 2: Magic Choose action:
Everything was fine until I got to the last bit of code to try to run it. Here's the error for when I try to run an attack:
Error:
Traceback (most recent call last): You attacked for 69 Points of damage. File "C:/Users/RhoJho/PycharmProjects/Battle/main.py", line 39, in <module> player.reduce_mp(cost) NameError: name 'cost' is not defined Process finished with exit code 1
I have tried to figure out how to define "cost" in this. In my instructor's Python, it runs fine. But when I try to run it, it pretends it has no idea what the hey I'm talking about.
But wait! There's more!
When I try to run magic here's the error I get:
Error:
Traceback (most recent call last): File "C:/Users/RhoJho/PycharmProjects/Battle/main.py", line 27, in <module> player.choose_magic() File "C:\Users\RhoJho\PycharmProjects\Battle\Classes\Game.py", line 74, in choose_magic print(str(i) + ":", spell["name"], "(cost:", str(spell["mp"]) + ")") KeyError: 'mp'
I seriously cannot figure out what in the name of sweet mother Mary I keep doing or not doing to have it come out like this. I tried to understand the other tutorial about finding out definitions, and I understand a little about functions, but I am not sure why my instructor's works like magic and mine does nothing but fart. Help please.
Reply
#2
on line 15, initialize cost as follows:
cost = 0
Reply
#3
Edit: forget what I just said. Now I'm getting this error message:
Error:
Traceback (most recent call last): File "C:/Users/Rhoho/PycharmProjects/Battle/main.py", line 40, in <module> You attacked for 52 Points of damage. enemy.take_damage(magic_dmg) NameError: name 'magic_dmg' is not defined
Why the hey am I not understanding any of this? Is there a resource that I need?
Reply
#4
no follow my post verbatim.
put:
cost = 0
on that line! This initializes the variable cost which will get it's real value later
Reply
#5
(Mar-08-2018, 05:26 PM)RhoJho Wrote:
    elif index == 1:
        player.choose_magic()
        magic_choice = int(input("Choose Magic:")) - 1
        magic_dmg = player.generate_spell_damage(magic_choice)
        spell = player.get_spell_name(magic_choice)
        cost = player.get_spell_mp_cost(magic_choice)
 
        current_mp = player.get_mp()
 
        if cost > current_mp:
            print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC)
            continue
 
    player.reduce_mp(cost)
    enemy.take_damage(magic_dmg)
    print(bcolors.OKBLUE + "\n" + spell + " deals", str(magic_dmg), "points of damage" + bcolors.ENDC)
I think those three lines at the bottom need to be indented another level. The previous block is where you define cost and magic_dmg, so it makes sense that those lines are only supposed to run for magic damage. Where they are now, outside the block, they'll run all the time for any kind of damage at all.
Reply


Forum Jump:

User Panel Messages

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