-3

I'm new to programming. I'm learning the walrus operator. I want the while loop end/stop when someone type quit or finish. Only quit works. when someone type "finish", it produces an output instead of existing the loop. Is there a way to end the while loop with both quit and finish?

When I print the variable foods, it gives the values True instead of the inputs(rice and wings). Is there a way for the walrus operator to show the actual input(rice, wings)

This is my code.

foods =list()
while food := input("what food do u like?: ") !="quit" != "finish":
    foods.append(food)

what food do u like?: rice
what food do u like?: wings
what food do u like?: finish
what food do u like?: quit

    foods
[True, True, True]

I tried watching several youtube videos and searching online but I didn't find the answer.

4
  • 6
    Please include your code as text in the question itself, rather than as a screenshot. Commented Sep 18, 2024 at 13:38
  • 1
    Welcome to Stack Overflow! Please edit your post to add code and data as text (using code formatting), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and many more reasons. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. See minimal reproducible example on what code is required. Commented Sep 18, 2024 at 13:44
  • Here is tutorial realpython.com/python-walrus-operator and peps.python.org/pep-0572/… Commented Sep 18, 2024 at 13:57
  • 1
    Walrus or not, double != like that, doesn't work either way. Commented Sep 18, 2024 at 16:03

1 Answer 1

2

Two problems:

  1. The assignment expression is executed after the rest of your statement, including the "exit" word checks, meaning the result of those checks are saved into your food variable, instead of the actual input string (this is why your list only contains Trues).

    You can use parentheses around food := input() to make sure that food is assigned to first:

    while (food := input()) ...
    
  2. Testing against multiple values with consecutive comparisons (i.e. != x != y) doesn't work: they are evaluated left-to-right, meaning you'll evaluate x != "value 1", then True != "value 2".

    The proper way to do something like this is using in or not in:

    while (food := input()) not in ["quit", "finish"]:
    

The code you have now does the following, in order:

  1. Gets the input
  2. Compares it against "quit"
  3. Compares the result of input != "quit" to "finish": True/False != "Finish", which is why "finish" doesn't exit the program.
  4. Assigns that True/False value to the food variable
  5. Continues looping if food is True

The fixed code would look something like this:

foods = list()
while (food := input("What food do u like? ")) not in ["quit", "finish"]:
    foods.append(food)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.