+ 1

person = {"Name": "Sarah", "Age":"18"}

Can someone break down this code and explain why it brings out 'a' please? print(person) outputs the result with the +18 value in. person = {"Name": "Sarah", "Age":"18"} person["Name"] = "Sia" person["Age"] = "18+" if '18+' not in person: print('a') else: print('b')

17th Oct 2022, 2:20 PM
Eren Kılıçlar
Eren Kılıçlar - avatar
2 Antworten
+ 4
the in operator check if a key is in the dictionary. It does not check for values. Since '18+' is a value, '18+' in person will be false. you might want to use the values() method of the dictionary if '18+' not in person.values(): print('a') else: print('b') but given the context, it's better to check for the age directly. if person['Age'] == '18+': I hope this helps :)
17th Oct 2022, 2:35 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
Makes sense now, thanks mate.
17th Oct 2022, 2:42 PM
Eren Kılıçlar
Eren Kılıçlar - avatar