0

Why this code gives me typeError? And how can I call a dictionary using varible X? "TypeError: string indices must be integers" I print "espresso" in input. Thanks

machine = {"Water": 300, "Milk": 200, "Coffe": 100,}
espresso = {"Water": 50, "Milk": 0, "Coffe": 28,}


def check(x):
   
    z = machine["Water"] >= x["Water"]
    print(z)

x = input()
check(x)

2 Answers2

0

use dictionary for mapping. this question

machine = {"Water": 300, "Milk": 200, "Coffe": 100,}
espresso = {"Water": 50, "Milk": 0, "Coffe": 28,}


def check(x):
    d ={'espresso':espresso} 
    z = machine["Water"] >= d[x]["Water"]
    print(z)

x = input()
check(x)
Tomáš Šturm
  • 489
  • 4
  • 8
  • yes. Could you please check again my message. I have edit it. The problem that I am calling to dictionary through function with input. – Raullock _ Dec 13 '21 at 12:36
-1

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

mkdahan
  • 3
  • 2
  • yes. Could you please check again my message. I have edit it. The problem that I am calling to dictionary through function with input. – Raullock _ Dec 13 '21 at 12:40