mardi 16 janvier 2018

Python 3.6: using conditional statement on a dictionary value

I need guidance on how to use a conditional statement on a value within a Python dictionary.

I want to run a function if the value of the key 'DrivingLicense', within the dictionary 'driver' is 'Yes' as shown below:

driver = {'name': 'John', 'DrivingLicense': 'Yes'}

if driver['DrivingLicense'] == 'Yes':
    doSomeThing()

This doesn't work as it produces the error:

TypeError: string indices must be integers

I've also tried using 'is' instead of '=='

driver = {'name': 'John', 'DrivingLicense': 'Yes'}

if driver['DrivingLicense'] is 'Yes':
    doSomeThing()

This produces the same error...

I am new to using dictionaries in Python so I may be missing understanding if/how dictionaries can be used within conditional statements...

Aucun commentaire:

Enregistrer un commentaire