0
profit = 0
resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

def enough_resources(ingredients) -> bool:
  flag = True
  for i in ingredients:
    if resources[i]-ingredients[i] < 0:
        flag = False
  return flag

def get_change(cost):
  print(f"Your coffee is ${cost:.2f}")
  print("Please insert coins")
  quarterMoney = int(input("How many quarters?: ")) * .25
  dimeMoney = int(input("How many dimes?: ")) *.1
  nickelMoney = int(input("How many nickels?: ")) * .05
  pennyMoney = int(input("How many pennies?: ")) * .01
  price = quarterMoney + dimeMoney + nickelMoney + pennyMoney
  if price < cost:
    print("not enough money")
    return
  else:
    global profit
    profit += cost
    print(f"Your total change is {price-cost}.")
    return

def use_resources(ingredients):
  for i in ingredients:
    resources[i] -= ingredients[i]
  return resources

As you can see, I have written this piece of code. What I don't understand it that in the function get_change, I have to declare the profit as a global variable. Why do I not have to do this when I use the resources dictionary? Please help. Is it because I am not modifying the dictionary?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

0 Answers0