-1

So here's what I'm trying to do:

The inputs will be in the correct syntax for Python. For example for this part:

conditionOne = input()

what the input will be something like "and 5 % 2 == 0"

So basically if conditionOne contains "and 5 % 2 == 0" and I have a statement which reads:

if exampleVariable == 1:

I need the program to get the string in the variable and put it into the if statement so it becomes

if exampleVariable == 1 and 5 % 2 ==0:

Is there any way to do this?

  • Take a look at the eval function, but I think something like this might do it `if eval('exampleVariable == 1' + conditionOne):` – dcg Nov 02 '17 at 17:12
  • 1
    I'm not sure if you could do something with [eval()](https://docs.python.org/3/library/functions.html#eval) here, because you're just passing in part of a statement rather than a whole statement. Your best bet might be to parse the string that's passed in. If you've got a limited set of things that could be in that string, it might be okay to write your own parser - https://tomassetti.me/parsing-in-python/ has some useful information on what the options are for parsing stuff in Python. – bouteillebleu Nov 02 '17 at 17:14
  • @bouteillebleu But when concatenated to `exampleVariable ==`, it will be a complete statement. – hyper-neutrino Nov 02 '17 at 17:18
  • Thank you all so much, eval() was what I was looking for here, will do a couple more tests to make sure it does the things I need. Thank you all again. – Improbablyaskingforhelp Nov 02 '17 at 17:18
  • I can't use it because it wants an "and" or an "or" after every condition, which was what the eval() function is supposed do to. Any help? For clarification I want it to be like "if 1 ==1 eval(var)" where var is equal to "and 2==2" @bouteillebleu – Improbablyaskingforhelp Nov 02 '17 at 17:25
  • 6
    Allowing users to enter arbitrary code to evaluate sounds like a poor design. Even if this is for your own personal use, you should still learn best practices. What are the reasons for building an if statement from user input? What is the original problem you are trying to solve with this? – Code-Apprentice Nov 02 '17 at 17:33
  • 3
    One question.. well one word sprung to my mind: "WHYYYY?!" – Antti Haapala -- Слава Україні Nov 02 '17 at 17:35
  • @Code-Apprentice There's a certain type of math problem that takes a little too long to solve by hand so I'm just inputting the conditions (ex: number can not be divided by 4) and having the computer do the calculations. – Improbablyaskingforhelp Nov 02 '17 at 17:36
  • 3
    @Improbablyaskingforhelp I'm pretty sure that you're either describing the fact that you don't fully understand your problem *or* you just want to have access to Python, in which case you should just have access to Python. You can [just launch the REPL](https://stackoverflow.com/a/1396199/344286) fwiw – Wayne Werner Nov 02 '17 at 17:36
  • @Improbablyaskingforhelp If that is the condition, just put it in the if statement. I don't see a need for you to type it every time you run the program. – Code-Apprentice Nov 02 '17 at 19:57

1 Answers1

1

If I understand you correctly you can do something like this:

# Initializing
conditionTotal = 'exampleVariable == 1'

conditionOne = input()

# Updating
conditionTotal = conditionTotal + ' ' + conditionOne

# If you want to execute it
exampleVariable = 1
if eval(conditionTotal):
    do_your_stuff()

But I wouldn't recommend you to code this way because you allow a user to enter any Python commands through the standard input that is completely unsafe because the user may execute an arbitrary Python program though yours.

Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
  • This is for my personal use, so it won't really matter. The thing is I can't have the eval command add an "and" because Python apparently wants something like an "or" or and "and" command after each condition. – Improbablyaskingforhelp Nov 02 '17 at 17:29
  • @Improbablyaskingforhelp So I would recommend you to take a look at the functions `eval` and `exec`: https://docs.python.org/3/library/functions.html#eval https://docs.python.org/3/library/functions.html#exec – Fomalhaut Nov 02 '17 at 17:31
  • 2
    "*Do this: [...] But I don't recommend it because it's unsafe*". Are you aware that SO is read by humans? – Andras Deak -- Слава Україні Nov 02 '17 at 17:36