mercredi 4 mars 2020

Python If-statement returning ValueError

I'm using Python3 to call an API to get a list of trains if certain conditions are met it sends me a notification on pushover.

The list I receive looks like this:

[
    {
        "Car": "8",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "4"
    },
    {
        "Car": "8",
        "Destination": "NewCrltn",
        "DestinationCode": "D13",
        "DestinationName": "New Carrollton",
        "Group": "1",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "6"
    },
    {
        "Car": "6",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "BRD"
    }
]

I have the following for loop and nested if statement to narrow down trains that are going to K08, don't have an 'ARR', 'BRD', or null for the 'Mins' variable, and within 10-20 mins:

for x in list:

        if x['DestinationCode'] == "K08":

                if x['Min'] != "" or "ARR" or "BRD":

                        if int(x['Min']) > 10 and int(x['Min']) < 20:
                                #pushover script

The first if statement works on the Destination variable. However, I get the following error on any with ARR, BRD, or null:

Traceback (most recent call last):
  File "./test.py", line 23, in <module>
    if int(x['Min']) > 10 and int(x['Min']) < 15:
ValueError: invalid literal for int() with base 10: 'BRD'

This leads me to believe that my nested if is passing 'ARR', 'BRD', and Null to the final if statement which will not accept anything but integers.

Aucun commentaire:

Enregistrer un commentaire