Reputation: 11
Trying to write a small script to make my life easier as I interface with the warehouse. Wanting to make something that will let me export a simple csv file with all the different styles, sizes, colors, and UPC codes of the products I need to fulfill. Here is part of the code that deals with getting the UPC codes from the dictionary I created:
UPC = {'True Premium Flat': {'White': {'6':'994000000446','7':'994000000453','8':'994000000460','9':'994000000477','10':'994000000484','11':'994000000491','12':'994000000507'},
'Silver': {'6':'994000000514','7':'994000000521','8':'994000000538','9':'994000000545','10':'994000000552','11':'994000000569','12':'994000000576'},
'Champagne': {'6':'994000000309','7':'994000000316','8':'994000000323','9':'994000000330','10':'994000000347','11':'994000000354','12':'994000000361'},
'Black': {'6':'994000000378','7':'994000000385','8':'994000000392','9':'994000000408','10':'994000000415','11':'994000000422','12':'994000000439'}
},
'Classic Flat': {'Black': {'Small':'994000000279','Medium':'994000000286','Large':'994000000293'},
'Champagne': {'Small':'994000000248','Medium':'994000000255','Large':'994000000262'},
}
}
def UPCget(St, C, Si):
return UPC[St][C][Si]
LineNum = raw_input('How many different items are returning? ')
Style = raw_input('Style? C or P: ')
if Style == 'C' or Style == 'c':
Style = 'Classic Flat'
if Style == 'P' or Style == 'p':
Style = 'True Premium Flat'
LineNum = int(LineNum)
for num in range(LineNum):
item = num + 1
print('\nItem number ' + str(item))
Color = raw_input('Color: ')
Size = raw_input('Size: ')
UPC = UPCget(Style, Color, Size)
print Color + ', Size ' + Size + ' has UPC code ' + UPC
f.close()
But I keep getting an 'string indices must be integers, not str' error only if my LineNum is greater than 1, and only on the second occurance. I've taken a look at the debugger, but can't seem to find any difference between when I call UPCget the first time VS the second time.
Would appreciate some help!
EDIT:
Forgot to post trackback :)
How many different items are returning? 2
Style? C or P: P
Item number 1
Color: Silver
Size: 7
Silver, Size 7 has UPC code 994000000521
Item number 2
Color: Champagne
Size: 7
Traceback (most recent call last):
File "/Users/klhuizinga/Documents/Talaria/ASN/UPCget.py", line 26, in <module>
UPC = UPCget(Style, Color, Size)
File "/Users/klhuizinga/Documents/Talaria/ASN/UPCget.py", line 12, in UPCget
return UPC[St][C][Si]
TypeError: string indices must be integers, not str
Upvotes: 1
Views: 99
Reputation: 76244
UPC = UPCget(Style, Color, Size)
Here's the problem. UPCget
works fine in the first iteration of your loop, but then UPC
gets overwritten. It's no longer a dict, now it's a string. Then in the second iteration it fails because you can't index a string the way UPCget
is doing.
Try using a different variable name so you don't overwrite the original value.
code = UPCget(Style, Color, Size)
print Color + ', Size ' + Size + ' has UPC code ' + code
Upvotes: 6