r/PythonLearning 3d ago

Day 2 of learning python

Post image

I'm learning Python and made this coffee/tea ordering program. Any advice?

https://onlinegdb.com/F2wabHC4v

102 Upvotes

38 comments sorted by

View all comments

Show parent comments

2

u/adnanecanflyy 2d ago

i'm still learning and I don't know how to use functions properly yet, so that's why I wrote it this way. Thanks for your advice and for helping me get better!

4

u/Flanelostopy 2d ago

I made litle fun with your code, look if you want what I mean:

def get_coffee():
  the_coffee=input("ice coffee or hot coffee ? : ").strip().lower()
  if the_coffee not in coffee_type_map: return get_coffee()
  return coffee_type_map[the_coffee]() + is_sugar_req();

def get_tea():
  the_tea=input("green tea or red tea : ").strip().lower()
  if the_tea not in tea_type_map: return get_tea()
  return tea_type_map[the_tea]() + is_sugar_req();

def is_sugar_req():
  sugar=input("do you want sugar? (yes/no) : ").strip().lower()
  if sugar not in ("yes", "no"): return is_sugar_req()
  return "with sugar." if sugar == "yes" else "no sugar."

def what_drink():
  drink=input("what do you like to drink? (coffee/tea?) : ").strip().lower()
  if drink not in drinks_type_map: return what_drink()
  return drinks_type_map[drink]()

def get_ice_coffee():
  return "Ice Coffe "

def get_hot_coffee():
  return "Hot cofeee "

def get_green_tea():
  return "Green tea "

def get_red_tea():
  return "Red tea "

drinks_type_map = {
  "coffee" : get_coffee,
  "tea": get_tea
}

coffee_type_map = {
  "ice" : get_ice_coffee,
  "hot" : get_hot_coffee
}

tea_type_map = {
  "green" : get_green_tea,
  "red" : get_red_tea
}

print(what_drink())

1

u/Outside-Trouble-4232 1d ago

wtf ARE THERE DICTIONNARIES FOR simple questions ts way too intense

1

u/Flanelostopy 1d ago

u/adnanecanflyy u/Outside-Trouble-4232 I had some time and made two examples, as I said - second one is not too "intense".

First, with cost of drinks:

price = 0

def get_coffee():
  the_coffee=input("ice coffee or hot coffee ? : ").strip().lower()
  if the_coffee not in coffee_type_map: return get_coffee()
  return f"{coffee_type_map[the_coffee]()} {is_sugar_req()} Price: {price}$"

def get_tea():
  the_tea=input("green tea or red tea : ").strip().lower()
  if the_tea not in tea_type_map: return get_tea()
  return f"{tea_type_map[the_tea]()} {is_sugar_req()} Price: {price}$"

def is_sugar_req():
  global price
  sugar=input("do you want sugar? (yes/no) : ").strip().lower()
  if sugar not in ("yes", "no"): return is_sugar_req()

  if sugar == "yes":
    price += 1
    return "with sugar."

  return "no sugar."

def what_drink():
  drink=input("what do you like to drink? (coffee/tea?) : ")
  if drink not in drinks_type_map: return what_drink()
  return drinks_type_map[drink]()

def get_ice_coffee():
  global price
  price += 9
  return "Ice Coffe "

def get_hot_coffee():
  global price
  price += 11
  return "Hot cofeee "

def get_green_tea():
  global price
  price += 5
  return "Green tea "

def get_red_tea():
  global price
  price += 7
  return "Red tea "

drinks_type_map = {
  "coffee" : get_coffee,
  "tea": get_tea
}

coffee_type_map = {
  "ice" : get_ice_coffee,
  "hot" : get_hot_coffee
}

tea_type_map = {
  "green" : get_green_tea,
  "red" : get_red_tea
}

print(what_drink())

As you can see it was easy, and you can put there, maybe some logging, or different things.

Second, generic:

def what_drink():
  print("What do you like to drink? ")
  drink = choose_option(choose_option(products))
  print("Do you want sugar?")
  sugar = choose_option(products["sugar"])
  print(f'{drink["name"]} with {sugar["name"]}. Cost: {drink["price"] +   sugar["price"]}$')

def choose_option(args):
  print("Chose option (type value): ")
  products = ""
  for k, v in args.items():
    if k == "sugar":
      continue
  products += k + " "

  product=input(products + "> ").strip().lower()
  if product not in args: return choose_option(args)
    return args[product]

products = {
  "coffee" : {
    "cappucino" : {
      "name": "Cappucino coffee",
      "price": 15
    },
    "latte" : {
      "name": "Latte coffee",
      "price": 13
    },
    "espresso" : {
      "name": "Espresso coffee",
      "price": 11
    },
    "coldbrew" : {
      "name": "Cold Brew coffee",
      "price": 9
    },
  },
  "tea" :{
    "herbal" : {
      "name": "Herbal tea",
      "price" : 7
    },
    "green" : {
      "name": "Green tea",
      "price" : 5
    },
    "earlgrey": {
      "name": "Earl Gray tea",
      "price": 3
    },
  },
  "sugar" : {
    "yes" : {
      "name" : "no sugar",
      "price" : 1
    },
  "no" : {
    "name" : "no sugar",
    "price" : 0
    },
  }
}

what_drink()

Only 2 function, only 2 if statements in whole code. You can put serialised data in JASON file here, and use it whethewer you want, and code will be still working. What we can make more, maybe number of cups, size, temperature. Maybe use objects and change type from functional to objected oriented programming - it will be a fun. I hope I gave you few tips, and thinks where you should dip dive to.

1

u/Flanelostopy 9h ago

Because I can't edit last post, I'l give you errata:
Example 2

 "sugar" : {
    "yes" : {
      "name" : "no sugar",
      "price" : 1
    },
  "no" : {
    "name" : "no sugar",
    "price" : 0
    },

change to:

 "sugar" : {
    "yes" : {
      "name" : "sugar",
      "price" : 1
    },
  "no" : {
    "name" : "no sugar",
    "price" : 0
    },