Follow us on Twitter!
The important thing is not to stop questioning. - Albert Einstein
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 20
Guests Online: 20
Members Online: 0

Registered Members: 82843
Newest Member: hx47
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Bit of help with Python


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 02-05-11 11:58
Basically, I want to be able for a user to input a string that includes the variables in my script (which they will know) and for them to be able to affect the script:
For example:

Code

i = 1
raw_input("Enter what you want to do to the variable 'i': ")
# some code that allows them to change the i variable
print i #the 'new' i




So if someone typed in i=i+5 or i= i*3
It would output 6 and 3..

Thanks in advance.


Author

RE: Bit of help with Python

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 02-05-11 12:47
You will want to use the exec() function.

Code

i = 0

x = raw_input("type in i and its assignment:\n example: i += 5\n>")

exec(x)

print(i)






www.userbars.com/74460/665255/337-7865-ubda3219.gif
kbcarte.wordpress.com
Author

RE: Bit of help with Python

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
Posted on 02-05-11 12:55
EDIT: Well techb's method is definitley a lot easier :P

The easiest way I can think of doing this is to use a regular expression to check if the string is of the correct form and then parse that string if the regex matches.

Code
import re
i = 1
str = raw_input("Enter what you want to do to the variable 'i': ")

# regular expression to match strings valid strings, e.g. "i=i+3" and "  i   =  4   / i   "
regex = re.match("[ ]*i[ ]*=[ ]*(i[ ]*[\+\-\*\/][ ]*[0-9]+|[0-9]+[ ]*[\+\-\*\/][ ]*i)[ ]*", str)
if regex:
    # get the individual parts of the string - operator, number, what order they were entered  (i-2 or 2-i)

    # could use regex:
    operator = re.search("[\+\-\*\/]", str).group(0)

    # or just iterate through string:
    for i in str:
        if i == "+" or i == "-" or i == "*" or i == "/":
            operator = i
    ...






Try a new search engine

Edited by starofale on 02-05-11 12:56
Author

RE: Bit of help with Python


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 02-05-11 13:22
Thanks for your replies, I'm going to use eval() Smile


Author

RE: Bit of help with Python

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 02-05-11 13:34
Note that eval only handles expressions, and exec only handles statements.

Check out this for better clarification.


www.userbars.com/74460/665255/337-7865-ubda3219.gif
kbcarte.wordpress.com
Author

RE: Bit of help with Python


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 02-05-11 14:32
Thanks, I'll have a look and decide later. Thanks again Smile