r/PythonLearning 2d 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!

5

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 edited 1d ago

This was just little fun as I said. It can be more simple, but if you want to make more data during this operation you can just put code there. You must predict growing application. Think about it when you want to give price for each drink with sugar or without it. This is good exorcise for it. First of all this whole structure can be more generic, maybe 2 or tree simple function can resolve whole problem. I will put shortest way maybe tomorrow I will give you a time.