I'm working on a small personal project where I turn a d&d story into a game.
I ran into a problem when the story reaches a point where a sheep runs to the character with the highest magic , and since I generate magic randomly at the start of the session , sometimes two characters can have the same magic value so I give it to the character with the highest charisma between the ones with the highest magic .. since I'm a beginner I tried to solve this with lists, a solution with dictionaries would be much appreciated but that's not my question, I came up with a solution that seems logical to my nooby eyes but if more than one character have equal charisma(regardless of magic values) the code fails and chooses randomly.
here is the code , what would be very useful is if I knew where the code bugs and why
since its completely invisible to me .
`
magic=[]
charisma=[]
ttt=["Khablan","Gadaan","El-Armoty" ,"Kulayb",'Zaafaran']
def attributeassigner(ww):
for x in ttt:
ww.append(random.randrange(1, 10))
return ww
print(attributeassigner(magic))
print(attributeassigner(charisma))
def maxfinderassigner(ba):
o=[k for k,x in enumerate(ba) if x == max(ba)]
return o
def anychooser(dodo):
e=ttt[dodo.index(max(dodo[t]for t in maxfinderassigner(magic)))]
return e
print(anychooser(charisma))
`
CodePudding user response:
The expression
max(dodo[t]for t in maxfinderassigner(magic))
computes the maximal charisma among characters with maximal magic. Then, however, you are searching for a character with this maximal charisma in a list of all characters - not necessarily the ones with maximal magic.
CodePudding user response:
This should fix the problem. It'll first check if there is someone with the highest magic. Then, it will check for someone with the highest charisma. If there are characters with the same amount of both, Tie
will be returned.
import random
magic = []
charisma = []
ttt = ["Khablan","Gadaan","El-Armoty" ,"Kulayb",'Zaafaran']
def attributeassigner(ww):
for x in ttt:
ww.append(random.randrange(1, 10))
return ww
print(attributeassigner(magic))
print(attributeassigner(charisma))
def anychooser():
if magic.count(max(magic)) == 1:
return ttt[magic.index(max(magic))]
if charisma.count(max(charisma)) == 1:
return ttt[charisma.index(max(magic))]
return "Tie"
print(anychooser())