Join us at IRC!
Things are more like they are now than they have ever been before. - Dwight D. Eisenhower
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 27
Web Spiders: 12
Guests Online: 26
Members Online: 1

Registered Members: 70169
Newest Member: fadzlin
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Bit of help with Python

SaMTHG
Member

Posts: 145
Location:
Joined: 27.03.08
Rank:
God
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 scblockedript (which they will know) and for them to be able to affect the scblockedript:
For example:


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.



There's no place on earth where there can be too much logic whatever form it's in.©
I rented a huge book from the library about mouth diseases-the colours were beautiful©[big]hello[/big]
http://www.black-zero.com
Sweet sig K_I_N_G thanks:D
This is like virtual life just without the sword's and helmet's.©
Author

RE: Bit of help with Python

techb
Member



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


i = 0

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

exec(x)

print(i)



kbcarte.wordpress.com
Author

RE: Bit of help with Python

starofale
Member



Posts: 211
Location: England
Joined: 05.12.07
Rank:
God
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 expresblockedsion to check if the string is of the correct form and then parse that string if the regex matches.

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

# regular expresblockedsion 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

SaMTHG
Member

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



There's no place on earth where there can be too much logic whatever form it's in.©
I rented a huge book from the library about mouth diseases-the colours were beautiful©[big]hello[/big]
http://www.black-zero.com
Sweet sig K_I_N_G thanks:D
This is like virtual life just without the sword's and helmet's.©
Author

RE: Bit of help with Python

techb
Member



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

Check out this for better clarification.


kbcarte.wordpress.com
Author

RE: Bit of help with Python

SaMTHG
Member

Posts: 145
Location:
Joined: 27.03.08
Rank:
God
Posted on 02-05-11 14:32
Thanks, I'll have a look and decide later. Thanks again :)



There's no place on earth where there can be too much logic whatever form it's in.©
I rented a huge book from the library about mouth diseases-the colours were beautiful©[big]hello[/big]
http://www.black-zero.com
Sweet sig K_I_N_G thanks:D
This is like virtual life just without the sword's and helmet's.©
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.