Why this code gives me typeError? And how can I call a dictionary using varible X? "TypeError: string indices must be integers" Thanks
machine = {"Water": 300, "Milk": 200, "Coffe": 100,}
espresso = {"Water": 50, "Milk": 0, "Coffe": 28,}
x = "espresso"
z = machine["Water"] >= x["Water"]
print(z)
CodePudding user response:
x is assigned a string "espresso", by using [] in x["Water"] you are trying to get index of "water" and it needs to be number. change x = espresso without "".
espresso = {"Water": 50, "Milk": 0, "Coffe": 28,}
#x = "espresso"
x= espresso
z = machine["Water"] >= x["Water"]
print(z)```
CodePudding user response:
because x holds: x = "espresso" and it is not a dictionary as you call it... you can't ask x["Water"].
you need to change from: x = "espresso" to x = espresso