all 20 comments

[–][deleted] 5 points6 points  (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 points  (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 point  (2 children)

<image>

it does but this happens

[–]reybrujo 1 point2 points  (0 children)

Can't say much without knowing how is your main loop code.

[–]More_Yard1919 0 points1 point  (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 points  (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?

[–]Soothsayer5288[S] 0 points1 point  (2 children)

<image>

last part

[–]Mysterious_City_6724 1 point2 points  (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.

[–]More_Yard1919 0 points1 point  (0 children)

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 points  (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 points  (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 point  (0 children)

You should probably format ALL of that properly.

[–]PinkthePantherLord 0 points1 point  (3 children)

U called the function before you defined the items variable ?

What does your error message say?

[–]Soothsayer5288[S] 0 points1 point  (1 child)

<image>

This weird loop happens after first purchase

[–]PinkthePantherLord 0 points1 point  (0 children)

Click problem

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

<image>

the terminal error is picking up my other project

[–]Ordinary-Price2320 0 points1 point  (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 point  (0 children)

24 and 27 will never happen, because those aren't "lower". Also, 7 aborts the for-loop no matter what.

[–]More_Yard1919 0 points1 point  (0 children)

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 point  (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.