0

Is there any way in python to input a condition from user? for example:

import numpy as np
m = np.array([0,1,2,3,4,5])
condition = input() # for example 'm > 50'
print( m [condition])  

I want a result of m[m>5] but I want to input this condition from user. Is there any way?

  • You can input numbers, string keys, but inputting variable names is a really bad and tricky practice. Can't your users directly use the python shell? – mozway Apr 22 '22 at 11:33

2 Answers2

2

Yes, there is a way. You can do it using the built-in eval() function but only if you can trust the input source. If you cannot, the user can supply harmful input that can corrupt your system. eval is known to be potentially very dangerous. eval takes its string argument, parses it and evaluates it as a Python expression. This can be an arbitrary Python expression. One concern is that Python permits users to access and delete system files (e.g. via the os module). So a bad-actor may supply dangerous data that you feed into eval, which could corrupt your system. This is why you need to be extra careful that you can trust the input source before supplying it to eval.

If you know, for example, that the condition will always be something like m ? x where ? is one of <, <=, >=, ==, >, then a safer approach is the following. You can first let the user input one of those options and check to make sure the user entered one of them. Then you can let the user enter the number x. You can then use eval to evaluate the concatenated string. For example,

import numpy as np

m = np.array([0,1,2,3,4,5])

select = {'<', '<=', '>', '>=', '=='}

sym = input("Enter one of <, <=, >, >=, ==: ")
if sym not in select:
    print("Invalid input")
    exit()
num = float(input("Enter number: "))

condition = eval('m ' + sym + repr(num))
print(m[condition])

Example Session

Enter one of <, <=, >, >=, ==: >
Enter number: 3
[4 5]

Example Session

Enter one of <, <=, >, >=, ==: harmful_code
Invalid input
OTheDev
  • 2,916
  • 2
  • 4
  • 20
-1

You can do something similar with eval, I think:

x = 5
condition = input('condition: ')
if eval(condition):
    print('yes')

And than in input write like x==5 or x>2 etc. and it should work.

theherk
  • 6,954
  • 3
  • 27
  • 52
ROOP AMBER
  • 330
  • 1
  • 8
  • @theherk I am very thankful to you, is there a way I can format code on this website with phone – ROOP AMBER Apr 22 '22 at 11:43
  • You can, but entering special characters like `\`` can be annoying. – theherk Apr 22 '22 at 11:45
  • @PedramPorbaha please upvote my answer too – ROOP AMBER Apr 22 '22 at 11:48
  • Keep in mind that using `eval` is a [**very bad practice**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice), in short **do not do this**. The other answer requires a bit more work and thinking but will be much more safe and robust (although not ideal as well). – mozway Apr 22 '22 at 11:56
  • @mozway ofcourse I agree with you that eval is pretty dangerous but I don't think he is using his console based project just for himself that is why I think he can use it although he can also demand the condition and do the needed programming ( like in your answer) – ROOP AMBER Apr 22 '22 at 12:01
  • You can do much better than `eval` with simple code. If OP is using code for themself, why not using the shell directly? That justification makes no sense. Btw, I did **not** provide an answer ;) – mozway Apr 22 '22 at 12:03
  • @mozway sorry I thought it was your answer bur it was oda's answer sometimes specially when we are beginners we want to do things like this for ourselves and impressing our parents although ofcourse it is dangerous – ROOP AMBER Apr 22 '22 at 12:05