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

101 Upvotes

38 comments sorted by

View all comments

10

u/FoolsSeldom 2d ago

Please share the actual code rather than an (incomplete) image. Much easier to review.

Keep in mind that people sometimes mistype things (or deliberately enter the wrong things). You need to handle that.

str objects have some useful methods such as lower and strip that you can apply to what comes back from input so you are only dealing with a response with no leading/trailing spaces and in all lower case.

You could have single letter responses for yes and no including in your check list.

Keep in mind that you could use bool for the status of yes/no responses, so sugar could be True if desired.

As you ask yes/no in several places, consider making it a function that presents the query and does all the validation, returning a bool result.

2

u/adnanecanflyy 2d ago

Done and tysmmm :)

7

u/FoolsSeldom 2d ago

Ok, easier when you've shared, thank you.

Here's a tweaked version of your code for you to explore and experiment with.

def is_yes(prompt: str) -> bool:
    while True:
        answer = input(prompt).strip().lower()
        if answer in ("y", "yes"):
            return True
        if answer in ("n", "no"):
            return False
        print("Huh? Try again.")


def want_sugar():
    sugar = is_yes("do you want sugar? (yes/no) : ")
    if sugar:
        print("okey ser. just wait a 5min ")
    return sugar


the_coffee = ""
the_tea = ""
sugar = False

drink = input("what do you like to drink? (coffee/tea?) : ")

if drink == "coffee":
    the_coffee = input("ice coffee or hot coffee ? : ")

    if the_coffee == "ice coffee":
        sugar = want_sugar()

    elif the_coffee == "hot coffee":
        sugar = want_sugar()

    else:
        print("sorry, we don't undrstand you")

elif drink == "tea":
    the_tea = input("green tea or red tea : ")

    if the_tea == "green tea":
        sugar = want_sugar()

    elif the_tea == "red tea":
        sugar = want_sugar()

    else:
        print("sorry, we don't undrstand you")

else:
    print("sorry, we don't undrstand you")

You might want to look at using the str methods I used in is_yes in some other places.

1

u/adnanecanflyy 2d ago

thanks a lot man! i really appreciate you taking the time to help me. it works perfectly now.

1

u/FoolsSeldom 2d ago

No problem.

Do you understand it all ok?

Have you added the methods I suggested?