0

I want to prevent the user from entering an input other than + , - , / , * , and the code I've written works fine and it prevents user from entering numbers, strings, spaces, but when user presses the ENTER KEY the program skips the input question and goes to the next code block.

How can I prevent a user from pressing ENTER KEY and skipping the "Enter an Operator: " input question?

op_error = True
while op_error:
    op_error = False
    op = input("Enter an Operator: ")
    if op in "+-/*":
        break
    elif len(op.strip()) == 0:
        op_error = True
    else:
        op_error = True
MP5
  • 23
  • 2
  • hi, might be difficult in a terminal shell environment. you could potentially use `stty igncr` to have the terminal ignore enter key. – IronMan Dec 21 '20 at 20:53
  • Can you specify a bit more precisely what your expected behavior versus observed behavior is? Looking at the code, if the input is anything other than "+ - * /" then it should just _go back_ to the input question. – cadolphs Dec 21 '20 at 20:53
  • Also, you don't need `op_error` in this code at all. Just do `while True:` and remove the `elif/else` clauses entirely. – Mad Physicist Dec 21 '20 at 20:54
  • @Lagerbaer I want the user to enter valid operators, if user inputs string, numbers, spaces it should reprompt them to the same question. However I couldn't prevent the user from simply pressing ENTER KEY which skips the input question. – MP5 Dec 21 '20 at 21:09
  • @MadPhysicist I want the user to input valid operators. If the user enters anything other than the specified operators it should reprompt them to the same question. However, if user presses ENTER KEY directly with no input it skips the question. I want the program to detect this problem. – MP5 Dec 21 '20 at 21:11
  • @MP5. Gotcha. Will answer momentarily. Your question is misleading because I read into it too much. – Mad Physicist Dec 21 '20 at 21:14
  • Does this answer your question? [detect key press in python?](https://stackoverflow.com/questions/24072790/detect-key-press-in-python) – Ken Kinder Dec 21 '20 at 21:19
  • 1
    Unless you've got some crazy exploit that blows up the user's keyboard or something, you're not going to be able to stop them from pressing whatever physical buttons they feel like. Trying to stop the user from pressing Enter is the wrong way to go about this. – user2357112 Dec 21 '20 at 21:19
  • `op in "+-/*"` is the wrong test. That's a substring check, not a membership check. Something like `"+-"` is a substring of `"+-/*`, and `""` in particular is a substring of any string. – user2357112 Dec 21 '20 at 21:22
  • Does this answer your question? [How to know if a user has pressed the Enter key using Python](https://stackoverflow.com/questions/23979184/how-to-know-if-a-user-has-pressed-the-enter-key-using-python) – BcK Dec 21 '20 at 21:24
  • @BcK. Not really a duplicate. – Mad Physicist Dec 21 '20 at 21:24
  • @KenKinder I'm not trying to detect keys pressed. I just want to prevent the user from skipping the question. Pressing ENTER KEY when the input question is asked skips it and doesn't give a valid value that the program can use. – MP5 Dec 21 '20 at 21:24
  • @martineau. I unmarked the duplicate in this case, because I think OP understands the basics of writing a loop for input. The question is really about the fact that `''` is in any other string. Please reclose if you deem necessary. – Mad Physicist Dec 21 '20 at 21:25
  • @MadPhysicist: You're probably right, but the answer is simply to do the length check first because the conditional `"" in "+-/*"` will always be `True` for the empty string. – martineau Dec 21 '20 at 21:33
  • 2
    @martineau. I would argue that the better method is to check against a sequence of distinct one-character strings, since you can have multi-character operators, among other benefits. That's why I bothered reopening. – Mad Physicist Dec 21 '20 at 21:43
  • @MadPhysicist: Good points…so I'll upvote your answer. `;¬)` – martineau Dec 21 '20 at 21:49

2 Answers2

3

The issue you are experiencing is with the test

if op in "+-/*":

Any string in the following list is going to pass, not just the empty string:

'', '+', '-', '/', '*', '+-', '-/', '/*', '+-/', '-/*', '+-/*'

The problem is that you are not checking if the string has length 1 before deciding if it is a valid operator. If you want to use only the in operator you can do:

if op in ('+', '-', '/', '*'):

Notice that '' and '+-' will never be equal to an element of that tuple. Another way is to explicitly check for length:

if len(op) == 1 and op in '+-/*':

As an aside, there is no need for the op_error variable in this case. It serves no logical purpose. Here is how you can rewrite the code in a much simpler way:

while True:
    op = input("Enter an Operator: ")
    if op in ('+', '-', '/', '*'):
        break

The advantage of the tuple approach is that it allows you to have multi-character operators (e.g. python's **). It will also make it much easier to transition to using a dictionary whose keys are the operators and whose values are, for example, processing hooks.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • This works, finally. I've been sitting here for 3 hours trying to figure it out. I started learning Python few days ago that is why there are some lines that serve no logical purpose, still a newbie :p You seem experienced, Thank you very much. – MP5 Dec 21 '20 at 21:35
  • One question, what's the problem with if statement passing +-? Is it equal to "" (empty string)? – MP5 Dec 21 '20 at 21:37
  • @MP5. `in` checks for a substring. `'+-'` is a substring of `'+-/*'`, just like `''` – Mad Physicist Dec 21 '20 at 21:42
-1

input will return an empty string if only enter is press. You can test for it by comparing with '' (two single quotes) or "" (two double quotes) with no space between them.

op_error = True
while op_error :
    op = input ('Enter an Operator: ')
    if op != '' and op in '+-/*' :
        op_error = False
print (op)
bashBedlam
  • 1,402
  • 1
  • 7
  • 11
  • This will still pass through `+-` for example. – Mad Physicist Dec 21 '20 at 21:26
  • @MadPhysicist That wasn't what the OP was asking about. The problem was that the Enter key was bypassing his test. – bashBedlam Dec 21 '20 at 21:29
  • That doesn't justify posting a solution that has another very obvious problem, especially one so closely related to the original. I would argue that the question is about invalid inputs passing in general, even if OP does not realize that it's more than just empty strings. – Mad Physicist Dec 21 '20 at 21:31