Home > database >  How do I compare list item values in a code, then add them together to find what items are most like
How do I compare list item values in a code, then add them together to find what items are most like

Time:09-28

I am looking to compare 1 list to another list. Then instead of getting a true or false answer, I would be able to then go ahead and add those values together. I am fairly new to this and I understand dictionaries, but I am having a hard time finding resources that would point in the right direction.

List:

mle = {"Weapon" : "fist", "Damage" : 73, "Fire Rate" : 80, "Range" : 59, "Accuracy" : 72, "Recoil" : 79, "Mobility" : 54, "Handling" : 51}

I would like to then compare to this list here:

brassKnucles = {"Attachment" : "brass", "Damage" : 3, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 3, "Recoil" : -2", "Mobility" : 5, "Handling" : 0}

I would like to add one item, Damage from "mle" to the damage of brassKnucles.

Conceptually I would do this

mle[2]   brassknucles[2]

Now while that is good, I have one more thing I want to do. I would like to add a third list.

rings = {"Attachment" : "rings", "Damage" : 1, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 5, "Recoil" : -6", "Mobility" : 7, "Handling" : 0}

Now I need an if statement and its hard to figure out how I will write it, I want to compare brassknuckles and rings, before adding to mle.

I know how to write out what I want to do, I am just having a hard time being able to put it on screen.

I tried this

def damage():
global a1, a2, a3, a4, a5, br1, br2, smg1, smg2, smg3, sg1, lmg1, lmg2, lmg3, mr1, mr2, sr, mle
if damage = "Damage":
    return
# Compare the stats
while(0==0):
    if mle[2] == 73:
        Print(mle)
        if brassKnucles[2] > rings[2]
            print(mle[2]   brassKnucles[2]
        else
            print("weapon"   mle[2]   "Attachment"   rings[2])
        break
        
return

Am I doing this correctly or am I just completely wrong?

CodePudding user response:

is this something you are looking for? In python with dictionaries you access the value via the key brassKnuclesDMG = brassKnucles["Damage"]

def damage():

    if damage == "Damage":
        return
    # Compare the stats
    while(True):
        if mle["Damage"] == 73:
            print(mle)
            if brassKnucles["Damage"] > rings["Damage"]:
                print(mle["Damage"]   brassKnucles["Damage"])
            else:
                print("weapon"   mle["Damage"]   "Attachment"   rings["Damage"])
            break

If you are interested this article can help you with the key (bu dum tss) concepts https://www.w3schools.com/python/python_dictionaries.asp

CodePudding user response:

I am not sure what the else statement is trying to do, but essentially if brassknucles (i called it bs) sustained more damage than rings then you are adding mle's damage value to bs. The code to do that is:

if bs["Damage"] > rings["Damage"]:
    print(mle["Damage"]   bs["Damage"])

I am guessing your else statement would be to add mle damage to rings instead if the statement above is false. If so, see code below:

else: print(mle["Damage"]   rings["Damage"])

CodePudding user response:

First attempt for programming is tricky, so with patience, we perhaps can start with familiar ourselves with the basic operations for accessing a dictionary:

mle = {"Weapon" : "fist", "Damage" : 73, "Fire Rate" : 80, "Range" : 59, "Accuracy" : 72, "Recoil" : 79, "Mobility" : 54, "Handling" : 51}
brassKnucles = {"Attachment" : "brass", "Damage" : 3, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 3, "Recoil" : -2, "Mobility" : 5, "Handling" : 0}
rings = {"Attachment" : "rings", "Damage" : 1, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 5, "Recoil" : -6, "Mobility" : 7, "Handling" : 0}

print(mle["Damage"])
print(mle["Damage"] brassKnucles["Damage"])
print(mle["Damage"] brassKnucles["Damage"] rings["Damage"])

Output:

73
76
77

to iter a dictionary

for k,v in mle.items():
    print(k,v)

Output:

Weapon fist
Damage 73
Fire Rate 80
Range 59
Accuracy 72
Recoil 79
Mobility 54
Handling 51

or

for k in mle:
    print(k,mle[k])

Output:

Weapon fist
Damage 73
Fire Rate 80
Range 59
Accuracy 72
Recoil 79
Mobility 54
Handling 51

Then try again to write what you want to process ;)

  • Related