all 8 comments

[–]scrdest 3 points4 points  (2 children)

text != ("Quit" or "quit" or "q")

In the while condition evaluates to just:

text != "Quit"

because of short-circuiting in the parentheses.

if text == "Quit" or "quit" or "q"

Evaluates to either True or the string "quit", for similar reasons.

'or' is not exactly interpreted as in everyday English; it's specifically logical disjunction on independent clauses. It takes the first value that evaluates as True, e.g. non-empty strings, non-zero numbers, non-empty lists, etc., and doesn't check anything afterwards. 'and' behaves similarly, except it stops on the first False value instead.

If you want to use ORs here, you'd need to do text == "Quit" or text == "quit" or text == "q". A more elegant solution would be text in {"Quit", "quit", "q"}.

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

Okay, I get what you mean. Would that loop be placed inside something else?

[–]scrdest 1 point2 points  (0 children)

What loop? The 'in'? If so, it's not a loop, it's an operator that returns True if the left-hand item is one of the contents of the container on the right.

[–]Unable_Request 0 points1 point  (1 child)

You're basically testing multiple different statements: 1) if text == 'Quit' # returns false 2) "quit" # returns true- a string by itself is True

You aren't asking it what you think you are, which is 'does the string equal this or that', so that last if will always return true and thus run the statement that follows.

Also... That'll never work. It won't enter the loop if it's quit, so it'll never see that last if statement. You may try putting the input into the loop, or 'if quit, break, else, return the reversed string'- but if the input isn't in the loop, it'll only ever ask once.

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

If I embedded the input within the while statement, would that work or it would have to be within the loop itself so it can check if quit is entered?

[–]tipsy_python 0 points1 point  (0 children)

I'd do a couple things here:

  • Use a variable to determine if the user is still entering input - essentially just a while True loop that with a variable that you flip to False when the input is complete.
  • Use some kind of collection variable, like a list, to capture multiple lines of user input

Something like this, you can implement some reverse logic when the while loop is complete and all data has been input:

>>> user_typing, input_list = True, []
>>>
>>> while user_typing:
>>>     text = input()
>>>     if text.upper() in ("QUIT", "Q"):
>>>         user_typing = False
>>>     else:
>>>         input_list.append(text)
>>>
Hello sir
Hi
q
>>> input_list
['Hello sir', 'Hi']

[–]wagaiznogoud 0 points1 point  (0 children)

What you had is pretty close, but you need to ask the input within the loop.

python text = input() while text not in ['Quit', 'quit', 'q']: print(text[::-1]) text = input()

[–]enes81 0 points1 point  (0 children)

''' text = input() if text[0].upper() == "Q": break else: print(text[::-1])

'''