Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to input a condition or a command from user in python?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading