# Thread: python conversion of string to integer

1. Gee! These Aren't Roasted!
Join Date
Jan 2012
Beans
161

## python conversion of string to integer

Hi guys Im working on a simple python script. This is what i have

Code:
```def fishing(bait, line_weight, pole_size):
print "You really want to go fishing dont you?"
raw_input()
print "The water is perfect for the %s bait you want to use" % bait
print "Don't want to use too heavy or too light line here. %d lb test weight might be too light.  Its a bit choppy. lets add 10 lbs to that and go with %d lb test" % (line_weight, line_weight + 10)
print "Now to match the pole to the line weight and the lure. The %s pole size that you specified shows that you are a master fisherman." % pole_size
print "You are the man! The %s combined with %d and the %s pole size are the combo of the masters! Now, get to work!" % (bait, line_weight + 10, pole_size)

fishing("bloodworms", 30, "medium")

print "Now to actually accept user input and return the same string"
raw_input()
print "Which type of bait are you using today?"
bait1 = raw_input("bait type>")
print "How about the line weight in lb?"
line_weight1 = raw_input("line weight>")
int lineweight = line_weight1
print "Ok, Got it.  Now for the pole, sir?"
pole_size1 = raw_input("pole size>")

fishing(bait1, lineweight, pole_size1)```
what i want to do is get an integer for the line weight and use it as the line_weight argument in the function fishing. How would i do that? THe way I have it isnt working......

2. ## Re: python conversion of string to integer

You want

Code:
`lineweight = int(line_weight1)`

3. Gee! These Aren't Roasted!
Join Date
Jan 2012
Beans
161

thank you!

4. ## Re: python conversion of string to integer

You will probably also want to put that inside a try block because you'll get a ValueError if someone enters "heavy", for example, as the line weight.

See section 8.3 for exactly that example:

http://docs.python.org/2/tutorial/errors.html

5. ## Re: python conversion of string to integer

Originally Posted by Bachstelze
You want

Code:
`lineweight = int(line_weight1)`
What's difference between that and this
Code:
`lineweight = eval(line_weight1)`

6. Tall Cafè Ubuntu
Join Date
Feb 2009
Beans
1,467

## Re: python conversion of string to integer

The difference is in what happens when some joker types "__import__('subprocess').call('rm -rf .'.split())" at the input prompt.
Last edited by trent.josephsen; February 6th, 2013 at 12:53 AM. Reason: Fixed malicious input to something that actually works

7. ## Re: python conversion of string to integer

I had once made that mistake of using eval() in an IRC bot. The end result was users could do whatever they wanted. They ended up making files on my HDD, etc. created their own python scripts, and was able to execute them, all from eval(). They ended up codepading sensitive files, etc. All which was my simple answer to a simple calculator expression, thus i would say eval() is bad. There is almost always a better way.

It was quite impressive watching them execute one liner python code from eval() and seeing the result, and process of giving myself a back door via eval(). Luckily they did no harm, just told me to never use eval() for that reason.

Code:
`eval("__import__('subprocess').Popen('ls')")`
Code:
`eval("open('testoneliner.txt','w').write('this on liner')")`
Last edited by micahpage; February 6th, 2013 at 01:43 AM.

8. ## Re: python conversion of string to integer

wow --- I've never thought of doing that. Thanks for the enlightenment.