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):
Here's the second part (main.py):
And here's the prompt that I got so far:
But wait! There's more!
When I try to run magic here's the error I get:
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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 |
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.