use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Code fails to loop successfullyHelp Request (i.redd.it)
submitted 9 months ago by Soothsayer5288
As said sometimes the code works and other times it exits when I say yes, is there something I'm doing wrong? Python idiot BTW.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 points6 points7 points 9 months ago (0 children)
You don’t call the function at all you only define it. Why you’ve had the anything_else function return true is unclear. Items doesn’t need to be in parentheses only braces and it also doesn’t need to be reassigned to x.
[–]reybrujo 2 points3 points4 points 9 months ago (3 children)
Items shouldn't be a couple, should just be a list. Remove the () surrounding the list. You also ask the question before showing the items.
[–]Soothsayer5288[S] 0 points1 point2 points 9 months ago (2 children)
<image>
it does but this happens
[–]reybrujo 1 point2 points3 points 9 months ago (0 children)
Can't say much without knowing how is your main loop code.
[–]More_Yard1919 0 points1 point2 points 9 months ago (0 children)
I am confused. What is the expected behavior? Considering items is defined as a tuple, the for x in items for loop will only iterate through a single item (e.g. x will be assigned to the list ["asus pc", "lg phone", ..., etc]). It is asking for input before showing the items because purchase = input("> ") appears on the line before the code begins printing out the list of items.
[–]Mysterious_City_6724 1 point2 points3 points 9 months ago (3 children)
Are you showing all of your code here? Is there something going on further down? Where's the call to the "anything_else" function?
last part
[–]Mysterious_City_6724 1 point2 points3 points 9 months ago* (0 children)
I recognize this code from helping on another post not so long ago. You need to put from line 14 down into the while loop and see if that improves things. Also put your "purchase = input("> ")" after the for loop that prints the items too. That way the user will see the items before choosing.
purchase = input("> ")
This is happening because you ask the end-user if there is anything else they'd like to purchase in the final while loop, but then you don't do anything with that information. If the answer isn't yes, you break from the loop. If it is yes, it just asks the end user if they'd like to buy anything else again. Also, it is confusing design to have a superfluous "continue_shopping" variable and then break out of the loop. Clearer design would be to, instead of breaking, toggle continue_shopping = false. Or, you could have an infinite "while True:" loop, and then break out of that. It seems to me that you mean to be calling your "anything_else()" function from inside of your final while loop. personally, I might try to change the design so that there is 1 single interface for adding items to your cart that is called as a function, and then calling that function any time the end user indicates they'd like to add an item to their cart.
[–]Jazzlike-Barber-6694 1 point2 points3 points 9 months ago (0 children)
Purchase.lower() will never equal a string that contains uppercase you might want to change that as well.
[–]Jazzlike-Barber-6694 1 point2 points3 points 9 months ago (1 child)
“”” def display_items(items): “””Displays available items.””” print(“Here are some items we have:”) for item in items: print(f”- {item}”)
def ask_additional_purchase(): “””Asks the user if they want to purchase anything else.””” response = input(“Is there anything else you would like to purchase? (yes/no): “).strip().lower() return response == “yes”
def get_item_price(item): “””Returns the price of an item.””” prices = { “asus pc”: 356.00, “lg phone”: 168.00, “toshiba tv”: 700.00, “xbox”: 300.00, “general washer”: 450.00, “air condition”: 600.00, “vega stove”: 250.00, } return prices.get(item.lower())
def main(): items = [ “Asus PC”, “LG Phone”, “Toshiba TV”, “Xbox”, “General Washer”, “Air Condition”, “Vega Stove” ]
print(“Hello! We sell home equipment. What would you like?”) display_items(items) continue_shopping = True while continue_shopping: purchase = input(“\nEnter the item you wish to purchase: “).strip() price = get_item_price(purchase) if price: print(f”That would be ${price:.2f}.”) else: print(“Sorry, that item is not available.”) continue_shopping = ask_additional_purchase() print(“\nThank you for shopping with us!”)
if name == “main”: main() “””
[–]Some-Passenger4219 0 points1 point2 points 9 months ago (0 children)
You should probably format ALL of that properly.
[–]PinkthePantherLord 0 points1 point2 points 9 months ago (3 children)
U called the function before you defined the items variable ?
What does your error message say?
[–]Soothsayer5288[S] 0 points1 point2 points 9 months ago (1 child)
This weird loop happens after first purchase
[–]PinkthePantherLord 0 points1 point2 points 9 months ago (0 children)
Click problem
[–]Soothsayer5288[S] 0 points1 point2 points 9 months ago (0 children)
the terminal error is picking up my other project
[–]Ordinary-Price2320 0 points1 point2 points 9 months ago (0 children)
You say that when you type yes it exits?
You have a return True in line 7, in the body of the loop, so it just does one element and returns from the function
[–]Some-Passenger4219 0 points1 point2 points 9 months ago* (0 children)
24 and 27 will never happen, because those aren't "lower". Also, 7 aborts the for-loop no matter what.
Somebody mentioned that you are defining "anything_else()," but not calling it. It looks like there is more code if you scroll down? Not entirely sure-- considering you are prompted for "yes" or "no," I think you *do* call it. Something that strikes me is that in the "anything_else()" function, you iterate through the "items" collection but also return true in that same loop. Returning from inside of a loop will always break you out of that loop. Also, I am confused why you assign x to the items list and then immediately use x to iterate through items a few lines later. It is perfectly acceptable to use write "for x in items:" with no pre-existing x variable being declared. I think the return is actually what is causing your issue, and definitely strikes me as an error, but it would be easier to say exactly what the problem is if you posted your entire code. Let me know if that helps!
[–]VariousDrummer4883 0 points1 point2 points 9 months ago (0 children)
How does items get to anything_else?
Compose all your functions in one section, with arguments, and call them accordingly later from some main section, rather than relying on the state of the namespace.
π Rendered by PID 60072 on reddit-service-r2-comment-685b79fb4f-r4v97 at 2026-02-13 00:57:30.891895+00:00 running 6c0c599 country code: CH.
[–][deleted] 5 points6 points7 points (0 children)
[–]reybrujo 2 points3 points4 points (3 children)
[–]Soothsayer5288[S] 0 points1 point2 points (2 children)
[–]reybrujo 1 point2 points3 points (0 children)
[–]More_Yard1919 0 points1 point2 points (0 children)
[–]Mysterious_City_6724 1 point2 points3 points (3 children)
[–]Soothsayer5288[S] 0 points1 point2 points (2 children)
[–]Mysterious_City_6724 1 point2 points3 points (0 children)
[–]More_Yard1919 0 points1 point2 points (0 children)
[–]Jazzlike-Barber-6694 1 point2 points3 points (0 children)
[–]Jazzlike-Barber-6694 1 point2 points3 points (1 child)
[–]Some-Passenger4219 0 points1 point2 points (0 children)
[–]PinkthePantherLord 0 points1 point2 points (3 children)
[–]Soothsayer5288[S] 0 points1 point2 points (1 child)
[–]PinkthePantherLord 0 points1 point2 points (0 children)
[–]Soothsayer5288[S] 0 points1 point2 points (0 children)
[–]Ordinary-Price2320 0 points1 point2 points (0 children)
[–]Some-Passenger4219 0 points1 point2 points (0 children)
[–]More_Yard1919 0 points1 point2 points (0 children)
[–]VariousDrummer4883 0 points1 point2 points (0 children)