So, I’m creating this simple battle system as a small project and at the end I need to check if the player has that monster in their team already, however I cannot seem to be able to check all of their team list at once.
Here is the details of the code (not the entire code just the basis for this problem):
monster1 = {
"name": "monster1",
"type": "Fire",
"hp": 78,
"atk": 52,
"def": 43,
"spAtk": 60,
"spDef": 50,
"spd": 65
}
monster2 = {
"name": "monster2",
"type": "Water",
"hp": 82,
"atk": 60,
"def": 32,
"spAtk": 48,
"spDef": 60,
"spd": 70
}
myteam = [monster1, monster2]
eMon = monster2
if eMon not in myteam:
print("You Won! Would you like to capture this monster?")
opt = input("y/n ")
I’ve tried using eMon[‘name’] instead, but it still gives the result as if it is not in the team even if it is.
any solution?
EDIT:
The entirety of the original code:
from os import openpty
import random
import time
import os
def clear():
os.system('clear')
fire1 = 1
fire2 = 0
water1 = 1
water2 = 0
grass1 = 1
grass2 = 0
fireMoves = [fire1, fire2]
waterMoves = [water1, water2]
grassMoves = [grass1, grass2]
moves = [fireMoves, waterMoves, grassMoves]
cyndaquil = {
"name": "Cyndaquil",
"type": "Fire",
"hp": 78,
"atk": 52,
"def": 43,
"spAtk": 60,
"spDef": 50,
"spd": 65
}
fennekin = {
"name": "Fennekin",
"type": "Fire",
"hp": 80,
"atk": 45,
"def": 40,
"spAtk": 62,
"spDef": 60,
"spd": 60
}
fireTypes = [cyndaquil, fennekin]
totodile = {
"name": "Totodile",
"type": "Water",
"hp": 100,
"atk": 65,
"def": 64,
"spAtk": 44,
"spDef": 48,
"spd": 43
}
froakie = {
"name": "Froakie",
"type": "Water",
"hp": 82,
"atk": 56,
"def": 40,
"spAtk": 62,
"spDef": 44,
"spd": 71
}
waterTypes = [totodile, froakie]
chikorita = {
"name": "Chikorita",
"type": "Grass",
"hp": 90,
"atk": 49,
"def": 65,
"spAtk": 49,
"spDef": 65,
"spd": 45
}
chespin = {
"name": "Chespin",
"type": "Grass",
"hp": 112,
"atk": 61,
"def": 65,
"spAtk": 48,
"spDef": 45,
"spd": 38
}
grassTypes = [chikorita, chespin]
allPoke = [fireTypes, waterTypes, grassTypes]
myteam = []
##ADDTOTEAM FUNC
def addToTeam(myteam, allPoke, x, y):
if allPoke[x][y]["name"] not in myteam:
myteam.append(allPoke[x][y])
print(f"{allPoke[x][y]['name']} has been added to your team!")
return
else:
print("Cannot add to Team")
return
##HPAFFECT FUNC
def hpAffect(pPoke, ePoke, affected, sp, pHealth, eHealth):
print(f"{pHealth} + {eHealth}")
if affected:
if sp:
pHealth -= (ePoke['spAtk'] - pPoke['spDef'] + 5)
return pHealth, eHealth
elif not sp:
pHealth -= (ePoke['atk'] - pPoke['def'] + 5)
return pHealth, eHealth
elif not affected:
if sp:
eHealth -= (pPoke['spAtk'] - ePoke['spDef'] + 5)
return pHealth, eHealth
if not sp:
eHealth -= (pPoke['atk'] - ePoke['def'] + 5)
return pHealth, eHealth
return pHealth, eHealth
return pHealth, eHealth
##ATTACK FUNC
def attack(pPoke, ePoke, thisMove, pHealth, eHealth):
eadd = random.randint(3, 7)
padd = random.randint(2, 6)
if thisMove == 0:
print(f"You used a Special {pPoke['type']} move!")
if pPoke['spAtk'] + padd > ePoke['spDef'] + eadd:
print("You won!")
pHealth, eHealth = hpAffect(pPoke, ePoke, False, True, pHealth, eHealth)
return pHealth, eHealth
elif pPoke['spAtk'] + padd < ePoke['spDef'] + eadd:
print("You lost...")
pHealth, eHealth = hpAffect(pPoke, ePoke, True, True, pHealth, eHealth)
return pHealth, eHealth
else:
print("You drew.")
return pHealth, eHealth
if thisMove == 1:
print(f"You used a Physical {pPoke['type']} move!")
if pPoke['atk'] + padd > ePoke['def'] + eadd:
print("You won!")
pHealth, eHealth = hpAffect(pPoke, ePoke, False, False, pHealth, eHealth)
return pHealth, eHealth
elif pPoke['atk'] + padd < ePoke['def'] + eadd:
print("You lost...")
pHealth, eHealth = hpAffect(pPoke, ePoke, True, False, pHealth, eHealth)
return pHealth, eHealth
else:
print("You drew.")
return pHealth, eHealth
##TURN FUNC
def turn(pPoke, ePoke, pHealth, eHealth, moves):
global allPoke
print("What do you wish to do?")
try:
opt = int(input("1. attack 2. heal "))
except ValueError:
print("Please choose a number.")
turn(pPoke, ePoke, pHealth, eHealth, moves)
if opt >= 2:
clear()
return
elif opt <= 1:
clear()
y = random.randint(0, 1)
if pPoke['type'] == "Fire":
thisMove = moves[0][y]
if pPoke['type'] == "Water":
thisMove = moves[1][y]
if pPoke['type'] == "Grass":
thisMove = moves[2][y]
pHealth, eHealth = attack(pPoke, ePoke, thisMove, pHealth, eHealth)
print(f"Your health is {pHealth} the enemies health is {eHealth}")
if pHealth <= 0:
print("You lost the battle! try again?")
opt = input("y/n ")
if opt == "y":
run(allPoke, myteam)
else:
return
if eHealth <= 0:
if len(myteam) > 5:
print("You won the battle! try again?")
opt = input("y/n ")
if opt == "y":
clear()
run(allPoke, myteam)
else:
return
else:
if ePoke not in myteam:
print("You Won! Would you like to capture this pokemon?")
opt = input("y/n ")
if opt == "y":
if ePoke['type'] == "Fire":
for i in range(len(allPoke[0])):
if allPoke[0][i]['name'] == ePoke['name']:
y = i
addToTeam(myteam, allPoke, 0, y)
print("try again?")
opt = input("y/n ")
if opt == "y":
run(allPoke, myteam)
else:
return
if ePoke['type'] == "Water":
for i in range(len(allPoke[1])):
if allPoke[1][i]['name'] == ePoke['name']:
y = i
addToTeam(myteam, allPoke, 1, y)
print("try again?")
opt = input("y/n ")
if opt == "y":
clear()
run(allPoke, myteam)
else:
return
if ePoke['type'] == "Grass":
for i in range(len(allPoke[2])):
if allPoke[2][i]['name'] == ePoke['name']:
y = i
addToTeam(myteam, allPoke, 2, y)
print("try again?")
opt = input("y/n ")
if opt == "y":
clear()
run(allPoke, myteam)
else:
return
print("try again?")
opt = input("y/n ")
if opt == "y":
clear()
run(allPoke, myteam)
else:
return
else:
run(allPoke, myteam)
else:
turn(pPoke, ePoke, pHealth, eHealth, moves)
##CALLTEAM FUNC
def callTeam(myteam, attribute):
for i in range(len(myteam)):
print(f"{i}: {myteam[i][attribute]}")
##RUN FUNC
def run(allPoke, myteam):
if len(myteam) < 1:
x = random.randint(0, 2)
y = random.randint(0, 1)
addToTeam(myteam, allPoke, x, y)
callTeam(myteam, "name")
x = random.randint(0, 2)
y = random.randint(0, 1)
ePoke = allPoke[x][y]
startBattle(ePoke, myteam)
##BATTLE FUNC
def startBattle(poke, myteam):
global moves
opt = None
print("Choose a pokemon to use:")
callTeam(myteam, "name")
try:
opt = int(input(""))
if myteam[opt] not in myteam:
print("Please choose a number that exists.")
startBattle(poke, myteam)
except ValueError:
print("Please choose a number.")
startBattle(poke, myteam)
pPoke = myteam[opt]
print(
f"You are battling a {poke['name']} and you are using a {pPoke['name']}")
turn(pPoke, poke, pPoke["hp"], poke["hp"], moves)
run(allPoke, myteam)
Thanks for the help, the code is now working as intended.
>Solution :
Solution:
I would gather the team names as strings then the rest of your code is basically the same. This seems to be the most simple way.
monster1 = {
"name": "monster1",
"type": "Fire",
"hp": 78,
"atk": 52,
"def": 43,
"spAtk": 60,
"spDef": 50,
"spd": 65
}
monster2 = {
"name": "monster2",
"type": "Water",
"hp": 82,
"atk": 60,
"def": 32,
"spAtk": 48,
"spDef": 60,
"spd": 70
}
my_team = [monster1, monster2]
my_team_names = [e_mon['name'] for e_mon in my_team]
e_mon = 'monster2'
if e_mon not in my_team_names:
print("You Won! Would you like to capture this monster?")
opt = input("y/n ")