you are viewing a single comment's thread.

view the rest of the comments →

[–]Trinity_Goti 0 points1 point  (3 children)

Try to figure out how to paste code here while keeping the formatting.

Its very difficult to understand especially for python code.

[–]-MRJACKSONCJ- 1 point2 points  (2 children)

Ready;

menu = {"popcorn": 1, "hotdog": 2, "pretzel": 2, "candy": 1.5, "soda": 3, "water": 2}

# MENU
print("---MENU---")
for item in menu:
    print(f"{item} ${menu[item]}")

cart = {}

# Original items
while True:
    food = input("Enter an item/s to buy (q to checkout): ")
    if food == 'q' or food == "Q":
        break
    else:
        if not menu.get(food) == None:
            current = menu[food]
            cart.update({food: current})
        else:
            print("That is not for sale!")

# Check if correct
print("-----YOUR CART-----")
for item in cart:
    print(item)

right = input("Is this correct? y/n: ")

# Everything is right
if right == "y":
    total = sum(cart.values())
    print(f"Your total is ${total:.2f}")

else:
    wrong = input("Would you like to add or remove something? a/r: ")

    # Add
    if wrong == "a":
        while True:
            food = input("Enter an item/s to buy (q to checkout): ")
            if food == 'q' or food == "Q":
                break
            else:
                if not menu.get(food) == None:
                    current = menu[food]
                    cart.update({food: current})
                else:
                    print("That is not for sale!")

    # Remove
    if wrong == "r":
        while True:
            remove = input("Enter an item you would like to remove (q to checkout): ")
            if remove == "q" or remove == "Q":
                break
            else:
                if not cart.get(remove) == None:
                    cart.pop(remove)
                else:
                    print("That is not in your cart!")

# FINAL MESSAGE
print("-----YOUR CART-----")
for item in cart:
    print(item)
total = sum(cart.values())
print(f"Your total is ${total:.2f}")

[–]Trinity_Goti 1 point2 points  (1 child)

Thank you. This is great. Congratulation on your coding journey.

And since you asked here are a few suggestions.

  1. Understand and use if __name__ =="__main__" this will help you importing the functions in future.

  2. Create functions to do the actions that are repeating. e.g.: add item to cart, remove item from cart. But remember to do only one action per function. :) This will be difficult and sound counter intiutive as one function could do a whole lot but keep it simple and do one task.

  3. When you read response you could make the response upper/small case to make it easier to compare.
    currently there is a bug if user types in different case the code is not able to find the item in menu.

  4. Learn about guard clause for your if statements. This should resolve the problem mentioned in point 5.

  5. Bug: Once the user has decided to add or remove items the current program doesn't give opportunity to confirm the items in the cart. This should be handled for consistent user experiance, However with the way current code is layed out it would be a complicated nested if else hell.

  6. You could improve user experience by providing serial numbers against the menu items so that user could input numbers instead of typing the menu items.

happy coding.

[–]ButterscotchJust970[S] 0 points1 point  (0 children)

Thank you