Python Forum

Full Version: Python code suddenly not working anymore
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a not too complicated python code that I have used pretty much every day until last week. It just reads an Excel file and lets me pick items and calculate a total (plus some other things). It sat in a folder on my desktop, and double clicking would bring up the application window every time.
Then I had to go on a trip and I moved the folder it was in temporarily off the desktop (last week). Today I restored the code tot he desktop, but double clicking it does not do anything. So I opened the code in Pythin IDLE and when I run it from there it suddenly generates error messages.

In 3.7 I receive the following error message:

File "C:\Users\Mike_b\Desktop\ProductSearch\Product_search.py", line 439, in <module>
App()
File "C:\Users\Mike_b\Desktop\ProductSearch\Product_search.py", line 152, in __init__
self.Types = sorted(tuple(self.productTypes))
TypeError: '<' not supported between instances of 'int' and 'str'

Again, the only thing I did was move the code to a different folder, then back to where it was before.

What could be the reason the code does not run anymore?

Thanks for your help.
Without seeing any code, it's hard to say. My first guess would be that the Excel spreadsheet has some string values in it where it didn't before (or integer values, it's not clear which it is supposed to be). I would look carefully at tuple(self.productTypes) to make sure it is what your program is expecting.
Thanks for responding. Here is the code immediately preceeding line 152. I understand that you still cannot tell which are string values and which are not, but #Loop through the excel file to create data structure of all desired values, but the error message says: '<' not supported between instances of 'int' and 'str', and I don't see a "<" operator in the code...
        self.CORPproducts = []
        self.productTypes = set()
            #Checks all values in the spreadsheet after the headers
        for row in tuple(self.ws.rows)[2:]:
            if row[0].value != None:
                    #if is a product type not inputted, create a new option for it
                if row[0].value not in self.productTypes:
                    self.productTypes.add(row[0].value)
                    newType = CORPProductList(row[0].value)
                    self.CORPproducts.append(newType)
                    
                    #Loop through the data structure to add entry to correct list
                for product in self.CORPproducts:
                        #For each line/product in the sheet add the entry into data structure
                    if product.Type == row[0].value:
                            #use fuction from data structre to add data to correct storage
                        self.CORPproducts[self.CORPproducts.index(product)].addProduct(
                                productType=row[0].value, productItem=str(row[1].value)+" - 
                                "+str(row[3].value),price1=row[5].value, price3=row[7].value, 
                                price4=row[8].value, country=row[19].value)
            #Sort the values alphabetically and set equal to the possible Types
        self.Types = sorted(tuple(self.productTypes))
I will check if anything in the structure of the sheet was changed.
Python uses < in the background when sorting things.
Thanks. I did find the issue:

There is a column in the sheet that is supposed to be string data. Someone made a mistake and entered a number, so the first element of the tuple was always type str, except one row, where it was int. That then threw the "sorted" function.

I tried to set this line:

productType=row[0].value, productItem=str(row[1].value)+" -

to

productType=str(row[0].value), productItem=str(row[1].value)+" -

(specifically typecast the productType to string, but that did not work. Why wouldn't that work?

But...Problem solved.

Thanks for the help.
I'm not sure why forcing it to string wouldn't help, if that's the column you are having the problem with. On the other hand, if they made a mistake in one column, the could make a mistake in another one.