Skip to content

Instantly share code, notes, and snippets.

@whinee
Last active November 13, 2023 09:30
Show Gist options
  • Save whinee/906acbdf0cf3b0c0f6148a405b40e9b8 to your computer and use it in GitHub Desktop.
Save whinee/906acbdf0cf3b0c0f6148a405b40e9b8 to your computer and use it in GitHub Desktop.
Comprog 1, Lab 5

README

I can't be bothered to make a video explanation of this, so have chatGPT explain it for you :3

Problem

image

Flowchart

flowchart TD
    a([start]) --> b[/input payment_mode/]
    b --> d[/print menu/]
    d --> c{payment_mode<br>is 1?}
        c --> |yes| e[/input items_num/]
            e --> ah((a))
            ag((a)) --> f{items_num<br>is 1?}
                f --> |yes| g[/input menu_item/]
                    g --> h{menu_item<br>an int?}
                        h --> |yes| i["balance = balance - menu[menu_item]"]
                            i --> j[/print You ordered.../]:::success
                            j --> l
                        h --> |no| k[/print Please choose.../]:::error
                            k --> l([end])
                f --> |no| u{items_num<br>is 2?}
                    u --> |yes| m[/input menu_item_1/]
                        m --> n{menu_item_1<br>an int?}
                            n --> |yes| o[/input menu_item_2/]
                                o --> p{menu_item_2<br>an int?}
                                    p --> |yes| q["balance = balance -<br>(menu[menu_item_1] +<br>menu[menu_item_2])"]
                                        q --> r[/print You ordered.../]:::success
                                        r --> l
                                    p --> |no| t[/print Please choose.../]:::error
                                        t --> l
                            n --> |no| s[/print Please choose.../]:::error
                                s --> l
                    u --> |no| v[/print Sorry, you.../]:::error
                        v --> l
        c --> |no| w{payment_mode<br>is 2?}
            w --> |yes| x[/input load_hmm/]
                x --> y{load_hmm<br>is 1?}
                    y --> |yes| z[/input load_amt/]
                        z --> aa{load_amt<br>is int?}
                            aa --> |yes| ab[/print Current balance.../]
                                ab --> e
                            aa --> |no| ac[/print load_amt is not valid.../]:::error
                                ac --> l
                    y --> |no| ae
            w --> |no| ad{payment_mode<br>is 3?}
                ad --> |yes| ae[/print Thank you!/]:::success
                    ae --> l
                ad --> |no| af[/print payment_mode is not valid/]:::error
                    af --> l

    classDef success color:#83ce9e,stroke:#6fc890
    classDef error color:#f3626b,stroke:#f14652
Loading
# Initialize Variables
balance: int = 1000
# Constants
menu: dict[str, int] = {
"Burger": 150,
"Fries": 150,
"Chicken": 250,
}
# Generated Constants
menu_list = list(menu)
len_menu = len(menu_list)
nums_items_menu: str = ",".join(
[str(idx) for idx, _ in enumerate(menu_list[:-1], start=1)]
) + f", and {len_menu}" # 1, 2, and 3
payment_mode = input(
"""Please choose mode of payment:
[1] Card
[2] Cash
[3] Cancel
> """
)
if payment_mode == "1":
pass # do nothing
elif payment_mode == "2":
print("Sorry, we are no longer accepting cash")
load_hmm = input("""Do you want to load your card?
[1] Yes
[2] No
> """)
if load_hmm == "1":
load_amt = input("Enter amount: ")
try:
load_amt_int = int(load_amt)
except ValueError:
print(f'"{load_amt}" is not a valid amount. Please try again.')
exit()
print(f"""Current Balance: {balance}
New Balance: {load_amt_int}""")
balance = load_amt_int
elif load_hmm == "2":
print("Thank you!")
exit()
else:
print(f'"{load_hmm}" is not a valid choice. Please try again.')
exit()
elif payment_mode == "3":
print("Thank you!")
exit()
else:
print(f'"{payment_mode}" is not a valid value. Please try again.')
exit()
print("Menu:")
for idx, (k, v) in enumerate(menu.items(), start=1):
print(f"[{idx}] {k}: {v}")
items_num = input("Enter how many item/s you need to purchase: ")
if items_num == "1":
menu_item = input("Please order from the menu: ")
try:
menu_item_int = int(menu_item)
except ValueError:
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
if (menu_item_int < 1) or (menu_item_int > len_menu):
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
menu_item_name = menu_list[menu_item_int-1]
balance = balance - menu[menu_item_name]
print(f"""You ordered {menu_item_name}
Your current balance is {balance}
Please wait for your order.""")
elif items_num == "2":
print("Please order from the menu.")
# Order 1
menu_item_1 = input("First Order: ")
try:
menu_item_int_1 = int(menu_item_1)
except ValueError:
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
if (menu_item_int_1 < 1) or (menu_item_int_1 > len_menu):
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
menu_item_name_1 = menu_list[menu_item_int_1-1]
# Order 1
menu_item_2 = input("First Order: ")
try:
menu_item_int_2 = int(menu_item_2)
except ValueError:
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
if (menu_item_int_2 < 1) or (menu_item_int_2 > len_menu):
print(f'Please choose from {nums_items_menu}. Thank you! Please try again.')
exit()
menu_item_name_2 = menu_list[menu_item_int_2-1]
balance = balance - (menu[menu_item_name_1] + menu[menu_item_name_2])
print(f"""You ordered {menu_item_name_1} and {menu_item_name_2}
Your current balance is {balance}
Please wait for your order.""")
else:
print("Sorry, you can only order from one to two items. Please try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment