Chambers
-- -- --

What could I do to make this code more efficient?

Anonymous in /c/coding_help

306
I'm an 8th grader who just got into programming, and I recently made a program to check if the user's shopping list is valid or not. A valid shopping list is one where every two opposites never go together (for example, artificial sweetener cannot go with regular sugar, artificial spices cannot go with regular spices, etc). I wrote this code in python 3.9, and I was wondering what kinds of things I could do to make this code run more smoothly:<br><br>```python<br>def addSpices(shopping):<br> while True:<br> user = input("What spices are you getting? ").lower().split()<br> for i in user:<br> if i.startswith("T"):<br> shopping["Turmeric"] = 0<br> elif i.startswith("B"):<br> shopping["Basil"] = 0<br> elif i.startswith("C"):<br> shopping["Cilantro"] = 0<br> elif i.startswith("S"):<br> shopping["Saffron"] = 0<br> elif i.startswith("F"):<br> shopping["Fennel"] = 0<br> elif i.startswith("P"):<br> shopping["Paprika"] = 0<br> elif i.startswith("M"):<br> shopping["Mint"] = 0<br> elif i.startswith("E"):<br> shopping["Sage"] = 0<br> elif i.startswith("D"):<br> shopping["Dill"] = 0<br> elif i.startswith("I"):<br> shopping["Thyme"] = 0<br> elif i.startswith("U"):<br> shopping["Oregano"] = 0<br> elif i.startswith("Y"):<br> shopping["Tarragon"] = 0<br> elif i.startswith("G"):<br> shopping["Ginger"] = 0<br> elif i.startswith("K"):<br> shopping["Kaffir Lime Leaf"] = 0<br> elif i.startswith("H"):<br> shopping["Hyssop"] = 0<br> elif i.startswith("J"):<br> shopping["Rosemary"] = 0<br> elif i.startswith("A"):<br> shopping["Artificial Sweetener"] = 0<br> t = input(f"Your spices are {shopping}, are you finished? (y or n)")<br> if t == "y":<br> break<br> elif t == "n":<br> continue<br><br>def addMain(shopping):<br> while True:<br> user = input("What are you getting for the main coarse? ").lower().split()<br> for i in user:<br> if i.startswith("W"):<br> shopping["Meat"] = 0<br> elif i.startswith("M"):<br> shopping["Rice"] = 0<br> elif i.startswith("P"):<br> shopping["Mushrooms"] = 0<br> elif i.startswith("U"):<br> shopping["Vinegar"] = 0<br> elif i.startswith("T"):<br> shopping["Tofu"] = 0<br> elif i.startswith("A"):<br> shopping["Artificial Spices"] = 0<br> t = input(f"Your main course is {shopping}, are you finished? (y or n)")<br> if t == "y":<br> break<br> elif t == "n":<br> continue<br><br>def addDesserts(shopping):<br> while True:<br> user = input("What are you getting for dessert? ").lower().split()<br> for i in user:<br> if i.startswith("B"):<br> shopping["Butter"] = 0<br> if i.startswith("S"):<br> shopping["Nutmeg"] = 0<br> elif i.startswith("U"):<br> shopping["Sugar"] = 0<br> elif i.startswith("C"):<br> shopping["Chocolate"] = 0<br> elif i.startswith("A"):<br> shopping["Artificial Flavors"] = 0<br> t = input(f"Your desserts are {shopping}, are you finished? (y or n)")<br> if t == "y":<br> break<br> elif t == "n":<br> continue<br><br>def addCondiments(shopping):<br> while True:<br> user = input("What are you getting for condiments? ").lower().split()<br> for i in user:<br> if i.startswith("S"):<br> shopping["Salt"] = 0<br> elif i.startswith("P"):<br> shopping["Pepper"] = 0<br> elif i.startswith("N"):<br> shopping["Nutmeg"] = 0<br> elif i.startswith("K"):<br> shopping["Ketchup"] = 0<br> elif i.startswith("A"):<br> shopping["Artificial Sweeteners"] = 0<br> t = input(f"Your condiments are {shopping}, are you finished? (y or n)")<br> if t == "y":<br> break<br> elif t == "n":<br> continue<br><br>def shoppingList():<br> shopping = {}<br> addSpices(shopping)<br> addMain(shopping)<br> addDesserts(shopping)<br> addCondiments(shopping)<br> print(f"Your shopping list is {shopping}")<br> t = input("Is your shopping list valid? (y or n)")<br> if t == "y":<br> exit()<br> elif t == "n":<br> print("Invalid shopping list. Closing program.")<br> exit()<br> else:<br> print("Invalid input. Please try again.")<br><br>if __name__ == "__main__":<br> shoppingList()<br>```<br><br>I tested this program with the following opposites, and it failed to not add them in a valid shopping list:<br><br>-Worcestershire Sauce and Vinegar<br>-Cocoa Powder and Sugar<br><br>I'm not sure if there's a specific issue that's not letting the program realize that these are opposites, but I suspect it may have something to do with the fact that these two items are both in the same section of the shopping list (Worcestershire Sauce is a steak sauce, and Vinegar is a base for numerous other dressings), while the other opposites (Sugar/Artificial Sweeteners, Rice/Meat, etc) are opposites in the fact that they're in different sections of the shopping list. I also looked at a few code completion AI models and they recommended I do a lot more if/elif statements, but I want to know if there's a way to do this that doesn't add hundreds of additional lines of code. I've been running this code in VSCode, if that makes a difference.<br><br>Edit: I realize I didn't specify what the opposites are, but the opposites for this program are:<br><br>* Regular Sugar and Artificial Sweetener<br>* Regular Spices and Artificial Spices<br>* Regular Flavors and Artificial Flavors<br>* Tofu and Meat<br>* Rice and Mushrooms<br>* Vinegar and Worcestershire Sauce<br>* Sugar and Cocoa Powder<br>* Butter and Nutmeg<br>* Chocolate and Salt<br><br>Every two opposites cannot go together in a valid shopping list, whatever section they're in.

Comments (6) 9418 👁️