Results 1 to 8 of 8

Thread: python conversion of string to integer

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    Cool 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"
    print "Ready?"
    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. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python conversion of string to integer

    You want

    Code:
    lineweight = int(line_weight1)
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jan 2012
    Beans
    161

    Re: python conversion of string to integer

    thank you!

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    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. #5
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: python conversion of string to integer

    Quote Originally Posted by Bachstelze View Post
    You want

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

  6. #6

    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. #7
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    236
    Distro
    Ubuntu 14.04 Trusty Tahr

    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. #8
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: python conversion of string to integer

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •