How to compare for the value of a key in two or more different dictionaries ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Dec 2018, 9:16 PM
Arushi Singhania
Arushi Singhania - avatar
9 Answers
+ 2
I'm not sure if I understood well, maybe you wanted something like this: https://code.sololearn.com/cVD8HSJ52JtL/?ref=app
3rd Dec 2018, 10:06 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 4
dρlυѕρlυѕ Thx a lot for help 🤗 🤗🤗
3rd Dec 2018, 11:19 PM
Arushi Singhania
Arushi Singhania - avatar
+ 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");
4th Dec 2018, 12:23 AM
Rishi Anand
Rishi Anand - avatar
+ 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 ??
3rd Dec 2018, 10:18 PM
Arushi Singhania
Arushi Singhania - avatar
+ 2
Can't you just go like: if dict1['health'] > dict2['health'] : or something like that?
3rd Dec 2018, 10:59 PM
HonFu
HonFu - avatar
+ 2
HonFu It's not working.
3rd Dec 2018, 11:18 PM
Arushi Singhania
Arushi Singhania - avatar
+ 2
Rishi Anand Thx a lot for this !!!
4th Dec 2018, 12:39 AM
Arushi Singhania
Arushi Singhania - avatar
+ 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 🤗🤗
3rd Dec 2018, 10:17 PM
Arushi Singhania
Arushi Singhania - avatar
+ 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.
3rd Dec 2018, 11:18 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar