pythonuser-interfacehashtablevariable-assignmentlookup

Python UI- Why can't I access my Hashtable and the objects within through this variable?


I have created a UI that works for all options except this one that I am having issues with. This option 3 is meant to print out a package status at a user-inputted time. The funny thing is, option 2 prints out all package statuses at an inputted time and works just fine. See option 2 below:

# Option 2
    elif option == "2":
        # checkTime = input function and use timedelta for correct format
        checkTime = input('Please enter a time in the form "00:00:00"')
        hour, minute, second = checkTime.split(":")
        rtime = datetime.timedelta(hours=int(hour), minutes=int(minute), seconds=int(second))
        # For all packages print status at certain time
        for i in range(40):
            pack = myHash.search(i + 1)
            print(pack)
            # At Hub
            if rtime < pack.timeLeftHub0:
                print('At Hub')
            elif rtime == pack.time_delivered:
                print('Delivered on Truck ' + str(pack.truck))
            elif rtime > pack.time_delivered:
                print('Delivered on Truck ' + str(pack.truck))
            else:
                print('En Route on Truck ' + str(pack.truck))

This works. So for option 3, the only difference should be the following, I thought-

  1. I have an input named checkPackage added as well (so the user can say what package status they want by inputting a package id)
  2. instead of the 'for i in range', I need something to link the package id inputted in checkPackage to the package object in my hash table.

My Hash table is called myHash and the package IDs are the keys. Please know I have used multiple myHash search functions using the package IDs in other areas of the code and it works properly, so that's not the issue. And thus why I am confused why it doesn't want to work here. Here's my code below for option 3:

# Option 3
    elif option == "3":
        checkPackage = input('Please enter a package using its package ID')
        checkTime = input('Please enter a time in the form "00:00:00"')
        hour, minute, second = checkTime.split(":")
        rtime = datetime.timedelta(hours=int(hour), minutes=int(minute), seconds=int(second))
        for package_id in checkPackage:
            pack = myHash.search(package_id)
            if rtime < pack.timeLeftHub0:
                pack.status = 'At Hub'
                print(str(pack.package_id) + ' package status at ' + str(rtime) + ' = ' + str(pack.status))
            elif rtime == pack.time_delivered:
                pack.status = 'Delivered'
                print(str(pack.package_id) + ' package status at ' + str(rtime) + ' = ' + str(pack.status))
            elif rtime > pack.time_delivered:
                pack.status = 'Delivered'
                print(str(pack.package_id) + ' package status at ' + str(rtime) + ' = ' + str(pack.status))
            else:
                pack.status = 'En Route'
                print(str(pack.package_id) + ' package status at ' + str(rtime) + ' = ' + str(pack.status))

Here's the outcome I get:

Choose an option (1,2,3, or 4): 3
Please enter a package using its package ID14
Please enter a time in the form "00:00:00"10:00:00
Traceback (most recent call last):
  File "C:\Users\alecc\PycharmProjects\pythonProject\Main.py", line 431, in <module>
    if rtime < pack.timeLeftHub0:
               ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'timeLeftHub0'

So obviously since option 2 DOES work- the error is I can't get option 3 to read 'pack' as the package object represented by checkPackage input (which is the package ID). I even tried doing the same 'for i in range' method I used in option 2 and instead of range, put checkPackages. Same error


Solution

  • OMG I finally figured it out!

    So yes, as I have said, you have to have pack = checkPackage so it reads your input. You CAN get rid of the for loop, but the myHash.search method I was using must have int inside it as you said, Michael, so it reads the input as an integer and not a string, AND it must be up in my input line, not separate:

     # Option 3
    elif option == "3":
        checkPackage = myHash.search(int(input('Please enter a package using its package ID')))
        checkTime = input('Please enter a time in the form "00:00:00"')
        hour, minute, second = checkTime.split(":")
        rtime = datetime.timedelta(hours=int(hour), minutes=int(minute), seconds=int(second))
        pack = checkPackage