+ 4
How to compare for the value of a key in two or more different dictionaries ??
I am creating a card game. So, I have two card in this code, a and b. They have their description in respective dictionaries. Now, I want to compare for the value of a key in both of them . For ex, by comparing the power in both, I want to know who is more powerful. How can I do this ?? https://code.sololearn.com/cWJFZ8rQuGpS/?ref=app
9 Respuestas
+ 2
I'm not sure if I understood well, maybe you wanted something like this:
https://code.sololearn.com/cVD8HSJ52JtL/?ref=app
+ 4
dρlυѕρlυѕ Thx a lot for help 🤗 🤗🤗
+ 3
You will be knowing keys to compare before hand
So you can do something as follows:
if(a.get("power")==b.get("power")):
print("both having same powers");
elif(a.get("power") > b.get("power")):
print("a is powerful");
else:
print("b is powerful");
+ 2
And also can u plz explain that zip function ?? I am unable to understand it. Is it something like zipping and unzipping a file ??
+ 2
Can't you just go like:
if dict1['health'] > dict2['health'] :
or something like that?
+ 2
HonFu It's not working.
+ 2
Rishi Anand Thx a lot for this !!!
+ 1
dρlυѕρlυѕ What can I do if I have to compare the second value that is health ?? What I need to edit ??
Thx a lot for help 🤗🤗
+ 1
Arushi Singhania For zip() take a look at this example, it's pretty simple: https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_func_zip.asp
As for the the code, I have updated it, but there's probably a much better way.