Can't call list from within class. - python

When I run the code I get the error message:
"AttributeError: type object 'Player' has no attribute 'hkslist'"
Why can't I call the list from within the class? The Idea is that the list is supposed to choose one of two of the functions from within the list and then run that once it's called upon.
Full code:
import random
from time import *
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Player:
coins = 100
health = 100
p = 3 #Potions
hoursslept = 0
def __init__(self):
random.seed()
print("init instance variables here")
#self.coins = random.randint(100, 500) Förlegad kod.
def hk1():
print("Du sprang iväg")
def hk2():
#print("Du försökte springa iväg men trillade och slog i knät: - 20 HP")
print("Du sprang iväg")
sleep(3)
print("men du trillar och slår i knät: -20 HP")
self.health = self.health - 20
print("Your health is: " + str(self.health), " HP")
#Fortsätt 'storyn'
def hkf1():
print("Du besegrade håkan")
print("Du tar hans droger och säljer det själv till Eleverna: +150 coins")
print("Your health is ", str(self.health), " HP")
self.coins = self.coins + 150
print("You have: ", str(self.coins), " coins")
def hkf2():
print("Håkan besegrade dig: -50 HP")
print("Your health is ", str(self.health), " HP")
print("You have: ", str(self.coins), " coins")
self.coins = self.coins + 150
self.hkslist = [hk1, hk2]
self.hkflist = [hkf1, hkf2]
self.indhks = random.randint(0,len(self.hkslist)-1)
self.indhkf = random.randint(0,len(self.hkflist)-1)
def report_status(self):
status_message = "Your health is %s, \nCoins left %s" % (self.health, self.coins)
return status_message
william = Player()
hakan = Player()
print("Welcome to a poorly made text-based game:")
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
print("You have: ", str(william.coins), " coins")
while True:
print("Commands: add, drink, coins, sleep, quest")
a = input("Enter a command: ")
if a == "add" or a == "Add" or a == "buy" or a == "Buy":
if william.coins == 0:
print("You can't afford a potion")
else:
william.coins = william.coins - 25
william.p = Player.p + 1
print("You bought a potion for 25 coins")
print("You have ", william.coins, " coins left")
print("you have ", william.p, " potions")
print("Your health is now ", str(william.health), " HP")
if a == "drink":
if Player.p == 0:
print("You can't drink any potions.")
print("You have zero potions left!")
print("Your health is ", str(william.health), " HP")
elif william.health >= 250:
print("Your health is already maxed out. You can't drink a potion.")
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
else:
william.health = william.health + 20
william.p = william.p - 1
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
if a == "sleep":
if william.health >= 250:
print("Your health is already maxed out. You can't drink a potion.")
print("Your health is ", str(william.health), " HP")
else:
william.health = william.health + 5
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
if a == "I" or a == "i" or a == "inv" or a == "inventory":
if william.p == 0:
print("Your backpack is empty")
else:
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
if a == "quest":
quest = input("Choose from quest: 1, 2 or 3 ")
if quest == "1" or quest == "option 1" or quest == "opt 1":
print("Du vandrar runt på Rudbeck när du ser Håkan, din samhällslärare, i ett mörkt hörn")
print("Du väljer att gå närmare för att investigera.")
print("Håkan står och säljer knark till eleverna.")
hk = input("Vad gör du? Spring: Slåss:")
if hk == "Spring" or hk == "spring":
Player.hkslist[Player.indhks]()
if hk == "slåss" or hk == "Slåss" or hk == "s":
Player.hkflist[Player.indhkf]()
if a == "coins":
if william.coins >= 500:
print("You're filthy rich!!!")
print("You have: ", str(william.coins), " Coins")
else:
print("You have: ", str(william.coins), " Coins")
#Debug tools
if a == "add coin" or a == "AC" or a == "ac" or a == "more":
extracoins = input("How many coins do you want?: ")
Player.coins = int(extracoins)
print("You now have: ", str(william.coins), " Coins")

hkslist is an attribute of an instance of Player not of the Player class itself. You can see this in the definition of hkslist (self.hkslist = [hk1, hk2]) where hkslist is defined on self. If you want to access hkslist you'll need to create an instance of Player. For example:
player = Player()
player.hkslist[player.indhks]()

Related

Why is it that the list variables in this code gets values in them backwards? HP is the last assigned variable despite being there first in the list

I'm making a simple program (I am a beginner at python) where I fight a monster with random assigned values. I used 2 lists with 4 variables each depicting hp atk def and spd. They get assigned a random number of 10 to 15 and get multiplied by 2. I don't really know what I am doing wrong here.
import random
monsters = ["slime","goblin","troll","dryad","bard","clown"]
hp,atk,dfn,spd = 0,0,0,0
mhp,matk,mdfn,mspd = 0,0,0,0
stats = [hp,atk,dfn,spd]
mstats = [hp,atk,dfn,spd]
damage = 0
defend = 0
action = "none"
name = input()
print("Your name is " + name + ", time to battle!")
for x in stats:
stats[x] = random.randint(10,15) * 2
print("Your stats(hp/a/d/s): " + str(stats[x]))
for y in mstats:
mstats[y] = random.randint(10,15) * 2
print("Monster stats(hp/a/d/s): " + str(mstats[y]))
while stats[hp] > 0 or mstats[hp] > 0:
print("What will you do? 1 for attack, 2 for defend, others to give up")
action = input()
if action == "1":
damage = stats[atk] / 2 + random.randint(1,5)
mstats[hp] = mstats[hp] - damage
print("You slash the monster for " + str(damage) + " damage!" )
print("Monster HP: " + str(mstats[hp]))
damage = mstats[atk] / 2
stats[hp] = stats[hp] - damage
print("The monster slashes you for " + str(damage) + " damage!")
print("Your HP: " + str(stats[hp]))
if action == "2":
damage = mstats[atk] / 2 - random.randint(3,5)
if damage > 0:
stats[hp] = stats[hp] - damage
print("The monster slashes you for " + str(damage) + " damage!")
print("Your HP: " + str(stats[hp]))
if action != "1" and action != "2":
stats[hp] = 0
if stats[hp] < 0 or stats[hp] == 0:
print("You lose!")
if mstats[hp] < 0:
print("You win!")
Hopefully this code isn't a mess, I thank you all in advance if extra corrections can be given.

use of global, but still not recognized in Python function

I was making a game where you need to fight monsters, just to practice my coding, when I got an error I couldn't understand. Here is the code, and the error I received:
Note: I changed it to the full code so you could see everything. Sorry about how long it is.
import random
import time
player_level = 1
player_HP = 4
player_mana = 2
player_action = "Placeholder"
entity_list = ["Nymph", "Komodo", "Free Soul"]
current_monster_level = player_level
current_monster_HP = random.randrange(current_monster_level, (current_monster_level * 3) + 1)
monster_name = entity_list[random.randrange(0, 3)]
def spawn_monster(level):
import random
global current_monster_HP
global monster_name
current_monster_HP = random.randrange(level, level * 3)
monster_name = entity_list[random.randrange(0, 3)]
print("A ", monster_name, "attacks you!")
print("The ", monster_name, " has ", current_monster_HP, " HP!")
def attack(level):
import random
global monster_name
global current_monster_HP
damage = random.randrange(level, level + 4)
current_monster_HP = current_monster_HP - damage
if damage == 0:
print("You missed! No damage done! ", monster_name, "'s HP is ", current_monster_HP, " !")
elif damage > current_monster_HP:
damage = current_monster_HP
print("You did ", damage, " damage! The ", monster_name, " was defeated!")
else:
print("You did ", damage, " damage. The enemy's HP is now ", current_monster_HP, "!")
def spell(level):
global player_mana
global player_HP
player_mana = player_mana - 2
player_HP = player_HP + level
print("A spell is cast, and you gain ", level, "HP!")
def monster_retaliate(level):
import random
global player_HP
global monster_name
global current_monster_HP
damage = random.randrange(level, level + 2)
player_HP = player_HP - damage
print("The ", monster_name, " attacked you, dealing ", damage, " damage!")
if damage == 0:
print("The enemy missed! No damage done! Your HP is ", player_HP, "!")
elif damage > current_monster_HP:
damage = current_monster_HP
else:
print("the ", monster_name, " did ", damage, " damage. Your HP is now ", player_HP, "!")
print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("x Blind RPG x")
print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("\n")
begin = input("Start a new game?").lower()
if begin == "yes":
print("Your tale starts when your memory does. You can't remember anything!")
time.sleep(1)
print("You wake up in an empty field, and you think you see a shadowy figure.")
time.sleep(1)
print("It slips away into the faraway trees lining the grassy field, or does it?")
time.sleep(1)
print("You stand up.")
time.sleep(1)
print("You decide to go over to where the shadow left the field.")
time.sleep(1)
print("On the ground, you see a small sword. You pick it up.")
time.sleep(1)
print("Suddenly, something jumps out at you from behind a tree.")
time.sleep(1)
spawn_monster(1)
while current_monster_HP > 0:
player_action = input("What do you do?").lower()
if player_action == "attack":
attack(player_level)
time.sleep(2)
if current_monster_HP > 0:
monster_retaliate(1)
else:
break
elif player_action == "cast spell":
spell(player_level)
time.sleep(2)
monster_retaliate(1)
else:
print("The enemy attacked you as you stood unmoving!")
monster_retaliate(1)
if current_monster_HP <= 0:
print("You win! Apparently you remember how to fight!")
break
if player_HP <= 0:
print("You died! If only you had remembered how to fight...")
break
else:
print("Then why did you open the game?")
The attack() function works, and the spell() function both work, it is only the monster_retaliate() function that is causing trouble. The Error code is:
File "/Users/(Replacing my name for privacy)/Documents/Blind RPG.py", line 57, in monster_retaliate
player_HP = player_HP - damage
NameError: name 'player_HP' is not defined
I thought this was funny, seeing as I used the global keyword
global doesn't actually create a variable; it just indicates that the name should be defined in the global scope, and to modify that (instead of a local variable) when you assign to it. You still need to give it a value before you try to access it.

Python Error: ValueError: Non-integer arg 1 for randrange()

I'm getting the above error in my code. It's just a simple text-based game.
Here's the code:
import os
import sys
import random
class Player:
def __init__(self, name):
self.name = name
self.maxhealth = 100
self.health = self.maxhealth
self.attack = 10
self.gold = 0
self.potions = 0
class Goblin:
def __init__(self, name):
self.name = name
self.maxhealth = 50
self.health = self.maxhealth
self.attack = 5
self.goldgain = 10
GoblinIG = Goblin("Goblin")
class Zombie:
def __init__(self, name):
self.name = name
self.maxhealth = 70
self.health = self.maxhealth
self.attack = 7
self.goldgain = 15
ZombieIG = Zombie("Zombie")
def main():
#os.system("cls")
print("Welcome to my game!")
print("1) Start")
print("2) Load")
print("3) Exit")
option = input("-> ")
if option == "1":
start()
elif option == "2":
pass
elif option == "3":
pass
else:
main()
def start():
#os.system("cls")
print("Hello, what is your name?")
option = input("-> ")
global PlayerIG
PlayerIG = Player(option)
start1()
def start1():
#os.system("cls")
print("Name: %s" % PlayerIG.name)
print("Attack: %s" % PlayerIG.attack)
print("Gold: %i" % PlayerIG.gold)
print("Potions: %i" % PlayerIG.potions)
print("Health: %i/%i" % (PlayerIG.health, PlayerIG.maxhealth))
print("1) Fight")
print("2) Store")
print("3) Save")
print("4) Exit")
option = input("-> ")
if option == "1":
prefight()
elif option == "2":
store()
elif option == "3":
pass
elif option == "4":
sys.exit()
else:
start1()
def prefight():
global enemy
enemynum = random.randint(1,2)
if enemynum == 1:
enemy = GoblinIG
else:
enemy = ZombieIG
fight()
def fight():
print("%s vs %s" % (PlayerIG.name, enemy.name))
print("%s's Health: %s/%s %s Health: %i/%i" % (PlayerIG.name, PlayerIG.health, PlayerIG.maxhealth, enemy.name, enemy.health, enemy.maxhealth))
print("Potions Left: %s\n" % PlayerIG.potions)
print("1) Attack")
#Implement defend system
print("2) Drink Potion")
print("3) Run")
option = input()
if option == "1":
attack()
elif option == "2":
drinkPotion()
elif option == "3":
run()
else:
fight()
def attack():
global PlayerAttack
global EnemyAttack
PlayerAttack = random.randint(PlayerIG.attack / 2, PlayerIG.attack)
EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)
if PlayerAttack == PlayerIG.attack / 2:
print("You missed!")
else:
enemy.health -= PlayerAttack
print("You dealt %i damage!" % PlayerAttack)
option = input(" ")
if enemy.health <= 0:
win()
if EnemyAttack == enemy.attack/2:
print("The enemy missed!")
else:
PlayerIG.health -= EnemyAttack
print("The enemy dealt %i damage!" % EnemyAttack)
option = input("")
if PlayerIG.health <= 0:
dead()
else:
fight()
def drinkPotion():
if PlayerIG.potions == 0:
print("You don't have any potions!")
else:
PlayerIG.health += 50
if PlayerIG.health >= PlayerIG.maxhealth:
PlayerIG.health = PlayerIG.maxhealth
print("You drank a potion!")
option = input(" ")
fight()
def run():
runnum = random.randint(1,3)
if runnum == 3:
print("You have ran away!")
option = input(" ")
start1()
else:
print("You've failed to run away!")
option = input(" ")
EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)
if EnemyAttack == enemy.attack/2:
print("The enemy missed!")
else:
PlayerIG.health -= EnemyAttack
print("The enemy dealt %i damage!" % EnemyAttack)
option = input(" ")
if PlayerIG.health <=0:
dead()
else:
fight()
def win():
enemy.health = enemy.maxhealth
PlayerIG.gold -= enemy.goldgain
print("You have defeated the %s!" % enemy.name)
print("You found %i gold!" % enemy.goldgain)
option = input(" ")
start1()
def dead():
print("You have died!")
option = input(" ")
def store():
pass
main()
I get the error in def run(), in the else: statement. The line that goes like: EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)
Any ideas? Should I elaborate more on my issue? Thanks :)
Use EnemyAttack = random.randint(math.floor(enemy.attack / 2), enemy.attack), or use math.ceil() to round the result of enemy.attack / 2.
randrange() expects whole integers. The error is occurring because the result of enemy.attack / 2 isn't a whole number; it has decimal places whenever enemy.attack is odd. One option is to scale randrange() after it is calculated if you need decimal results.
For example: 0.5*randrange(2*(enemy.attack/2), 2*enemy.attack), which can be simplified into 0.5*randrange(enemy.attack, 2*enemy.attack)

List Index Is Out of Range And I Don't Know Why [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So this is the error:
Traceback (most recent call last):
File "E:\python\cloud.py", line 593, in <module>
inventory()
File "E:\python\cloud.py", line 297, in inventory
print("Weapon Attack Damage: ", c.weaponAttack[i])
IndexError: list index out of range
These are the only parts of the code that have the "weaponAttack" function in it. I honestly don't see why it is giving me this error.
class Cloud:
def __init__(self):
self.weaponAttack = list()
self.sp = 0
self.armor = list()
self.armorReduction = list()
self.weapon = list()
self.money = 0
self.lvl = 0
self.exp = 0
self.mexp = 100
self.attackPower = 0
self.hp = 100
self.mhp = 100
self.name = "Cloud"
c = Cloud()
armors = ["No Armor","Belice Armor","Yoron's Armor","Andrew's Custom Armor","Zeus' Armor"]
armorReduce = [.0, .025, .05, .10, .15]
c.armor.append(armors[0])
c.armorReduction.append(armorReduce[0])
w = random.randint(0, 10)
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
print("You have recieved the ", weapons[w])
print("")
print("It does ", weaponAttack[w]," attack power!")
print("")
for i in range(0, len(c.weapon)):
print(i)
print("Weapon: ", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
Although, this is the rest of the code, but before you read it, I'm warning you, it's long. Either way, I'm pretty sure that those lines of code up there are the problem.
import random
import time
import sys
def asky():
ask = input("Would you like to check you player stats and inventory or go to the next battle? Say inventory for inventory or say next for the next battle: ")
if "inventory" in ask:
inventory()
elif "next" in ask:
user()
def Type(t):
t = list(t)
for a in t:
sys.stdout.write(a)
time.sleep(.02)
class Cloud:
def __init__(self):
self.weaponAttack = list()
self.sp = 0
self.armor = list()
self.armorReduction = list()
self.weapon = list()
self.money = 0
self.lvl = 0
self.exp = 0
self.mexp = 100
self.attackPower = 0
self.hp = 100
self.mhp = 100
self.name = "Cloud"
c = Cloud()
armors = ["No Armor","Belice Armor","Yoron's Armor","Andrew's Custom Armor","Zeus' Armor"]
armorReduce = [.0, .025, .05, .10, .15]
c.armor.append(armors[0])
c.armorReduction.append(armorReduce[0])
w = random.randint(0, 10)
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
print("You have recieved the ", weapons[w])
print("")
print("It does ", weaponAttack[w]," attack power!")
print("")
class Soldier:
def __init__(self):
dmg = random.randint(5,20)
self.lvl = 0
self.attackPower = dmg
self.hp = 100
self.mhp = 100
self.name = "Soldier"
s = Soldier()
def enemy():
ad = random.randint(0,2)
if ad >= 1: #Attack
Type("Soldier attacks!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
hm = random.randint(0, 2)
if hm == 0:
Type("Miss!")
print("")
elif hm > 0:
crit = random.randint(0,10)
if crit == 0:
print("CRITICAL HIT!")
crithit = int((s.attackPower) * (.5))
c.hp = c.hp - (s.attackPower + crithit)
elif crit >= 1:
c.hp = c.hp - s.attackPower
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
asky()
c.exp = c.exp + 100
else:
user()
elif ad == 0:#Defend
Type("Soldier Defends!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if s.hp == s.mhp:
print("")
elif s.hp > (s.mhp - 15) and s.hp < s.mhp:
add = s.mhp - s.hp
s.hp = add + s.hp
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
elif s.hp < (s.mhp - 15):
s.hp = s.hp + 15
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
asky()
c.exp = c.exp + 100
else:
user()
def user():
User = input("attack or defend? ")
if "attack" in User:#attack
Type("Cloud attacks!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
hm = random.randint(0,4)
if hm == 0:
Type("Miss!")
print("")
elif hm > 0:
crit = random.randint(0,7)
if crit == 0:
print("CRITICAL HIT!")
crithit = int((c.weaponAttack[0]) * (.5))
s.hp = s.hp - (c.weaponAttack[0] + crithit)
elif crit >= 1:
s.hp = s.hp - c.weaponAttack[0]
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
c.exp = c.exp + 100
asky()
else:
enemy()
elif "defend" in User:#defend
Type("Cloud Heals!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp == c.mhp:
Type("You are at the maximum amount of health. Cannot add more health.")
print("")
elif c.hp > (c.mhp - 15) and c.hp < c.mhp:
add = c.mhp - c.hp
c.hp = add + c.hp
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
elif c.hp <= (c.mhp - 15):
c.hp = c.hp + 15
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("Congratulations!")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
c.exp = c.exp + 100
asky()
else:
enemy()
else:
Type("The option you have entered is not in the game database. Please try again")
print("")
user()
def inventory():
if c.exp == c.mexp:
print("LEVEL UP!")
c.exp = 0
adde = int((c.mexp) * (.5))
c.mexp = c.mexp + adde
c.sp = c.sp + 1
c.lvl = c.lvl + 1
if c.lvl > s.lvl:
s.lvl = s.lvl + 1
print("Level: ", c.lvl)
nextlvl = c.lvl + 1
print("Experience: ", c.exp, "/", c.mexp, "level", nextlvl)
print("Amount of Skill Points:", c.sp)
for i in range(0, len(c.weapon)):
print(i)
print("Weapon: ", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
for j in range(0, len(c.armor)):
print("Armor: ", c.armor[j])
print("Armor Damage Reduction: ", c.armorReduction[j])
print("")
print("Amount of Crystals: ", c.money)
print("")
print("")
print("Stats:")
print("Maximum Health: ", c.mhp)
print("Current Health: ", c.hp)
print("Your Name: ", c.name)
print("")
sn = input("To heal yourself, you need to go to the shop. Say, *shop* to go to the shop, say *name* to change your name, say, *next* to fight another battle, say, *level* to use your skill point(s), or say, *help* for help: ")
if "name" in sn:
c.name = input("Enter Your name here: ")
print("Success! Your name has been changed to ", c.name)
inventory()
elif "next" in sn:
Type("3")
print("")
Type("2")
print("")
Type("1")
print("")
Type("FIGHT!")
print("")
user()
elif "help" in sn:
Type("The goal of this game is to fight all the enemies, kill the miniboss, and finally, kill the boss! each time you kill an enemy you gain *crystals*, currency which you can use to buy weapons, armor, and health. You can spend these *crystals* at the shop. To go to the shop, just say *shop* when you are in your inventory. Although, each time you defeat an enemy, they get harder to defeat. Once you level up, you gain one skill point. This skill point is then used while in your inventory by saying the word *level*. You can use your skill point(s) to upgrade your stats, such as, your maximum health, and your attack power.")
print("")
inventory()
elif "shop" in sn:
shop()
elif "level" in sn:
skills()
else:
print("Level: ", c.lvl)
nextlvl = c.lvl + 1
print("Experience: ", c.exp, "/", c.mexp, "level", nextlvl)
print("Amount of Skill Points:", c.sp)
for i in range(0, len(c.weapon)):
print("Weapon:", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
for i in range(0, len(c.armor)):
print("Armor: ", c.armor[i])
print("Armor Damage Reduction: ", c.armorReduction[i])
print("")
print("Amount of Crystals: ", c.money)
print("")
print("")
print("Stats:")
print("Maximum Health: ", c.mhp)
print("Current Health: ", c.hp)
print("Attack Power: ", c.attackPower)
print("Your Name: ", c.name)
print("")
sn = input("To heal yourself, you need to go to the shop. Say, *shop* to go to the shop, say *name* to change your name, say, *next* to fight another battle, say, *level* to use your skill point(s), or say, *help* for help: ")
if "name" in sn:
c.name = input("Enter Your name here: ")
print("Success! Your name has been changed to ", c.name)
inventory()
elif "next" in sn:
Type("3")
print("")
Type("2")
print("")
Type("1")
print("")
Type("FIGHT!")
print("")
user()
elif "help" in sn:
Type("The goal of this game is to fight all the enemies, kill the miniboss, and finally, kill the boss! each time you kill an enemy you gain *crystals*, currency which you can use to buy weapons, armor, and health. You can spend these *crystals* at the shop. To go to the shop, just say *shop* when you are in your inventory. Although, each time you defeat an enemy, they get harder to defeat. Once you level up, you gain one skill point. This skill point is then used while in your inventory by saying the word *level*. You can use your skill point(s) to upgrade your stats, such as, your maximum health, and your attack power.")
print("")
inventory()
elif "shop" in sn:
shop()
elif "level" in sn:
skills()
def skills():
print("You have ", c.sp, "skill points to use.")
print("")
print("Upgrade attack power *press the number 1*")
print("")
print("Upgrade maximum health *press the number 2*")
print("")
skill = input("Enter the number of the skill you wish to upgrade, or say, cancel, to go back to your inventory screen.")
if "1" in skill:
sure = input("Are you sure you want to upgrade your character attack power in return for 1 skill point? *yes or no*")
if "yes" in sure:
c.sp = c.sp - 1
addsap = c.attackPower * .01
c.attackPower = c.attackPower + addsap
if "no" in sure:
skills()
elif "2" in skill:
sure = input("Are you sure you want to upgrade your maximum health in return for 1 skill point? *yes or no*")
if "yes" in sure:
c.sp = c.sp - 1
c.mhp = c.mhp + 30
if "no" in sure:
skills()
elif "cancel" in skill:
inventory()
else:
Type("The word or number you have entered is invalid. Please try again.")
print("")
skills()
def shop():
print("Welcome to Andrew's Blacksmith! Here you will find all the weapons, armor, and health you need, to defeat the horrid beast who goes by the name of Murlor! ")
print("")
print("Who's Murlor? *To ask this question, type in the number 1*")
print("")
print("Can you heal me? *To ask this question, type in the number 2*")
print("")
print("What weapons do you have? *To ask this question, type in the number 3*")
print("")
print("Got any armor? *To ask this question, type in the number 4*")
print("")
ask1 = input("Enter desired number here or say, cancel, to go back to your inventory screen. ")
if "1" in ask1:
def murlor():
Type("Murlor is a devil-like creature that lives deep among the caves of Bricegate. He has been terrorising the people of this village for centuries.")
print("")
print("What is Bricegate? *To choose this option, type in the number 1*")
print("")
print("Got any more information about this village? *To choose this option, type in the number 2*")
print("")
print("Thank you! *To choose this option, type in the number 3*")
ask3 = input("Enter desired number here, or say, cancel, to go back to the main shop screen. ")
if "1" in ask3:
Type("That's the name of this town.")
murlor()
elif "2" in ask3:
def askquest1():
quest1 = input("Well I DO know that there's this secret underground dungeon. It's VERY dangerous but it comes with a hug reward. If you ever concider it, could you get my lucky axe? I dropped it down a hole leading to the dungeon and i was too afraid to get it back. *If you accept the quest, say yes, if you want to go back, say, no.*")
if "yes" in quest1:
quest1()
elif "no" in quest1:
murlor()
else:
Type("The option you have selected is not valid. Please try again")
print("")
askquest1()
elif "3" in ask3:
shop()
else:
Type("The number or word you have entered is invalid. please try again.")
print("")
elif "2" in ask1:
def heal():
Type("Sure! That'll be 30 crystals.")
ask2 = input(" *say, okay, to confirm the purchase or say, no, to cancel the pruchase*")
if "okay" in ask2:
if c.money < 30:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
shop()
elif c.money >= 30:
c.money = c.money - 30
Type("30 crystals has been removed from your inventory.")
print("")
addn = c.mhp - c.hp
c.hp = c.hp + addn
Type("You have been healed!")
print("")
shop()
elif "no" in ask2:
shop()
else:
Type("The option you have chosen is invalid. Please try again")
print("")
heal()
elif "3" in ask1:
def swords():
print("Swords: ")
print("The Belice Sword: *Type 1 for this sword*")
print("Damage: 18")
print("Cost: 70 crystals")
print("")
print("The Sword of A Thousand Truths: *Type 2 for this sword*")
print("Damage: 28")
print("Cost: 100 crystals")
print("")
print("Spyro's Sword: *Type 3 for this sword*")
print("Damage: 32")
print("Cost: 125 crystals")
print("")
print("The Sword Of The Athens: *Type 4 for this sword*")
print("Damage: 36")
print("Cost: 150 crystals")
print("")
print("Coming Soon...")
sword = input("Enter the sword ID number or say cancel to go back to the main shop screen. You now have ", c.money, "crystals.")
if "1" in sword:
if c.money < 70:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 70:
c.money = c.money - 70
Type("70 crystals has been removed from your inventory.")
print("")
weapon.append("The Belice Sword")
Type("The Belice Sword has been added to your inventory!")
print("")
swords()
elif "2" in sword:
if c.money < 250:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 250:
c.money = c.money - 250
Type("250 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Sword Of A Thousand Truths")
Type("The Sword Of A Thousand Truths has been added to your inventory!")
print("")
swords()
elif "3" in sword:
if c.money < 525:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 525:
c.money = c.money - 525
Type("525 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Spyro's Sword")
Type("The Spyro's Sword has been added to your inventory!")
print("")
swords()
elif "4" in sword:
if c.money < 1050:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 1050:
c.money = c.money - 1050
Type("1050 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Sword Of The Athens")
Type("The Sword Of The Athens has been added to your inventory!")
print("")
swords()
elif "cancel" in sword:
shop()
else:
Type("The number or word you have entered is invalid. Please try again.")
print("")
swords()
elif "4" in ask1:
def armory():
print("Armor:")
print("Belice Armor, ID: 1")
print("Damage Reduction: 2.5%")
print("Cost: 100 crystals")
print("")
print("Yoron's armor, ID: 2")
print("Damage Reduction: 5%")
print("Cost: 250 crystals")
print("")
print("Andrew's Custom Armor, ID: 3")
print("Damage Reduction: 10%")
print("Cost: 500 crystals")
print("")
print("Zeus' Armor, ID: 4")
print("Damage Reduction: 15%")
print("Cost: 1000 crystals")
print("")
print("Coming Soon...")
print("")
armor = input("Enter armor ID number, or type, cancel, to go back to the main shop menu.")
if "1" in armor:
if c.money < 100:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 100:
c.money = c.money - 100
Type("100 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Belice Armor")
Type("Belice Armor has been added to your inventory!")
print("")
armory()
elif "2" in armor:
if c.money < 250:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 250:
c.money = c.money - 250
Type("250 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Yoron's Armor")
Type("Yoron's Armor has been added to your inventory!")
print("")
armory()
elif "3" in armor:
if c.money < 500:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 500:
c.money = c.money - 500
Type("500 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Andrew's Custom Armor")
Type("Andrew's Custom Armor has been added to your inventory!")
print("")
armory()
elif "4" in armor:
if c.money < 1000:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 1000:
c.money = c.money - 1000
Type("1000 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Zeus' Armor")
Type("Zeus' Armor has been added to your inventory!")
print("")
armory()
elif "cancel" in armor:
shop()
else:
Type("The word or number you have entered is invalid. Please try again")
armory()
elif "cancel" in ask1:
inventory()
else:
Type("The number or word you have entered is invalid. Please try again.")
print("")
shop()
inventory()
I think your problem is here:
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
That last line should probably be:
c.weaponAttack.append(weaponAttack[w])

Variable does not change in IF statement

I've tried to make a very simple RPG combat system.
But there's an error when the health goes below 0 and does nothing:
jack_battle_loop_1 = True
while jack_battle_loop_1:
battle_menu()
choise = input("> ")
if choise == "1":
#(jack_health + jack_defense) - my_damage = jack_health
jack_health = (jack_health + jack_defense) - random.randrange(20, 25)
#Processing the updated enemy health
print(deskemon + " HAVE INFLICTED " + str(random.randrange(20, 25)) + " TO CHIPHEAD")
print("CHIPHEAD HAS " + str(jack_health) + " HITPOINTS LEFT!")
time.sleep(1)
print("...")
time.sleep(1)
print("...")
time.sleep(1)
print("...")
time.sleep(1)
#(my_health + my_defense) - jack_damage = my_health
my_health = (my_health + my_defense) - jack_damage
print("CHIPHEAD HAVE INFLICTED " + str(jack_damage) + " TO " + deskemon)
print(deskemon + " HAS " + str(jack_health) + " HITPOINTS LEFT!")
jack_battle_loop = True
elif jack_health <= 0:
jack_battle_loop = False
elif my_health <= 0:
jack_battle_loop = False
elif choise == "":
jack_battle_loop = True
But instead, it outputs this:
TIMOHA HAVE INFLICTED 23 TO CHIPHEAD
CHIPHEAD HAS -24 HITPOINTS LEFT!
The elif statements are only executed if choise != "1". It seems the ifs that check health should be independent of the ifs that check the choices.
Something like
if choice == "1":
# do stuff
if jack_health <= 0 or my_health <= 0:
jack_battle_loop = False
Try this after your if choise == "1": line:
jacksdamage=random.randrange(20, 25)
jack_health = (jack_health + jack_defense) - jacksdamage
if jack_health<=0:
print "Jack recieved %.i damage and was killed!"%(jacksdamage)
break
else:
print "Jack took %.i damage. from CHIPHEAD."%(jacksdamage)
print "Jack has %.i hitpoints left."%(jack_health)
for _ in range(5):
time.sleep(1)
print "..."
mydamage=random.randrange(20,25)
my_health = (my_health + my_defense) - mydamage
if my_health<=0:
print deskemon+' took %.i damage from jack and was killed!'%(mydamage)
break
else:
print deskmon+' took %.i damage from jack.'%(mydamage)
print deskmon+' has %.i health left.'%(my_health)
for _ in range(5):
time.sleep(1)
print "..."

Categories