I am pretty sure the grammar used within my title is incorrect, hopefully my example will make more sense. Is the below code correct syntax?
class food:
def __init__(self):
initDict = {
"fruit": ['apple', 'orange', 'blueberry'],
"all_foods": "fruit" + ['broccoli','carrot']}
self.Inputs = initDict
def idk(self):
print(self.Inputs['all_foods'])
f = food()
Basically, I am trying to make all_foods contain everything that’s in fruit and add to the string.
I agree with and get the same error as @Thierry Lathuille:
Traceback (most recent call last):
File "C:\Users\Temp\2/ipykernel_18476/990716159.py", line 1, in <module>
f = food()
File "C:\Users\Temp\2/ipykernel_18476/3991279104.py", line 5, in __init__
"all_foods": "fruit" + ['broccoli','carrot']}
TypeError: can only concatenate str (not "list") to str
>Solution :
FWIW
class food:
def __init__(self):
fruits = ['apple', 'orange', 'blueberry']
all_foods = fruits + ['broccoli','carrot']
self.inputs = {
"fruit": fruits,
"all_foods": all_foods,
}
def idk(self):
print(self.inputs["all_foods"])
f = food()