So I made a game and I'm trying to create an inventory for the player, and I can't change a variable called "equipped_weapon". I've tried everything and or I get an Exception or when printing the player's inventory it shows the previous weapon. I'm a beginner too so if I'm missing something please tell me.
# Existing items
# Need a funtion to create a random item with random stats and a random name within a certain range.
yes_list = ["yes", "yeah", "sure", "why not"]
add_attack = "Attack:"
add_defense = "Defense:"
add_HP = "HP added:"
rarity = "Rarity"
weapon_rusty_sword = {
"Name:": "Rusty Sword",
add_attack: 1,
add_defense: 0,
rarity: 1
}
weapon_sword_wooden = {
"Name:": "Wooden Sword",
add_attack: 1.5,
add_defense: 0,
rarity: 1
}
weapon_sword_iron = {
"Name:": "Iron Sword",
add_attack: 2,
add_defense: 0,
rarity: 2
}
armor_chest_rusty_mail = {
"Name:": "Rusty Mail",
add_attack: 0,
add_defense: 1,
rarity: 1
}
armor_legs_adventurers = {
"Name:": "Adventurer's Leggings",
add_attack: 0,
add_defense: 1,
rarity: 1
}
armor_legs_rash_leggings = {
"Name:": "Rash Leggings",
add_attack: 0,
add_defense: 0.5,
rarity: 1
}
armor_head_rusty_cap = {
"Name:": "Rusty Cap",
add_attack: 0,
add_defense: 0.5,
rarity: 1
}
potion_hp = {
"Name:": "HP Potion,",
add_HP: 4
}
class Person:
#global equipped_weapon
equipped_weapon = weapon_rusty_sword
ui_equipped_weapon = {"Equipped Weapon: {}".format(weapon_rusty_sword): ""}
equipped_armor = {
"Head Protection:": {"Name": armor_head_rusty_cap["Name:"], "Defense": armor_head_rusty_cap[add_defense]},
"Chest Protection:": {"Name": armor_chest_rusty_mail["Name:"], "Defense": armor_chest_rusty_mail[add_defense]},
"Legs Protection:": {"Name": armor_legs_rash_leggings["Name:"], "Defense": armor_legs_rash_leggings[add_defense]},
}
ATTACK = 1 + equipped_weapon[add_attack]
DEFENSE = 1
HP = 20
gold = 10
potions = {"Potions: ": [potion_hp["Name:"]]}
ui_gold = {"Gold: ": gold}
def __init__(self):
self.name = "Cavalex"
inv = [
equipped_armor,
ui_equipped_weapon,
potions,
ui_gold
]
def see_inventory(self):
for element in self.inv:
for k, v in element.items():
if type(v) == list:
print(k, ' '.join(v))
else:
print(k, v)
# def equip_weapon(self, new_weapon_code):
# global equipped_weapon
# eq_val = input(
# "Do you want to equip this weapon? ->( {} )<-\nNote that your current weapon ( {} )will be discarded.".format(new_weapon_code["Name:"], self.equipped_weapon["Name:"]))
# if eq_val.lower() in yes_list:
# #del self.equipped_weapon
# self.equipped_weapon = new_weapon_code
# print("The weapon you had was discarded.")
# else:
# print("The new weapon was discarded.")
# See total amount of defense.
def defense_points():
return sum(value["Defense"] for key, value in player.equipped_armor.items())
def add_gold(amount):
player.gold += amount
# Adding to inventory
def new_potion_to_inv(potion):
player.potions["Potions: "].append(potion["Name:"])
def equipp_weapon(new_weapon_code):
# global player.equipped_weapon
eq_val = input(
"Do you want to equip this weapon? ->( {} )<-\nNote that your current weapon ( {} )will be discarded.".format(
new_weapon_code["Name:"], player.equipped_weapon["Name:"]))
if eq_val.lower() in yes_list:
del player.equipped_weapon
player.equipped_weapon = new_weapon_code
print("The weapon you had was discarded.")
else:
print("The new weapon was discarded.")
player = Person()
# game loop
while True:
print("Your name: ", player.name)
player.see_inventory() # Can't put print this function, else will print "None" in the end.
print("\nThis is your total armor defense: {}".format(defense_points()))
print()
new_potion_to_inv(potion_hp)
player.see_inventory()
print(player.ATTACK)
print()
print("Now we are changing your weapon.")
equipp_weapon(weapon_sword_iron)
print()
print(player.ATTACK)
player.see_inventory()
break
and this is the output:
C:\Users\mateu\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py
Your name: Cavalex
Head Protection: {'Name': 'Rusty Cap', 'Defense': 0.5}
Chest Protection: {'Name': 'Rusty Mail', 'Defense': 1}
Legs Protection: {'Name': 'Rash Leggings', 'Defense': 0.5}
Equipped Weapon: {'Name:': 'Rusty Sword', 'Attack:': 1, 'Defense:': 0, 'Rarity': 1}
Potions: HP Potion,
Gold: 10
This is your total armor defense: 2.0
Head Protection: {'Name': 'Rusty Cap', 'Defense': 0.5}
Chest Protection: {'Name': 'Rusty Mail', 'Defense': 1}
Legs Protection: {'Name': 'Rash Leggings', 'Defense': 0.5}
Equipped Weapon: {'Name:': 'Rusty Sword', 'Attack:': 1, 'Defense:': 0, 'Rarity': 1}
Potions: HP Potion, HP Potion,
Gold: 10
2
Now we are changing your weapon.
Do you want to equip this weapon? ->( Iron Sword )<-
Note that your current weapon ( Rusty Sword )will be discarded.yes
Traceback (most recent call last):
File "C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py", line 158, in <module>
equipp_weapon(weapon_sword_iron)
File "C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py", line 139, in equipp_weapon
del player.equipped_weapon
AttributeError: equipped_weapon
Process finished with exit code 1
see_inventorycool to see the whole thing you are working on, going on this problem in a minute