Beginner: String into Integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tizer
    New Member
    • Nov 2009
    • 1

    Beginner: String into Integer

    I'm trying to change a string into an integer,
    What I'm trying to do it something like this:

    A = 3 #So A is an integer of 3
    B = raw_input(">")
    > A # So now B is a string of A
    int(B) #This gives a error that I don't really understand. :S
    print B # With out the int(B) will print A, but I want it to print 3

    Any help on how to fix the int function so it'll do that?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Code:
    >>> A = 3
    >>> B = raw_input(">")
    >>> B
    'A'
    >>> int(eval(B))
    3
    >>>

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      eval is a very powerful function, and one of the advantages of scripting languages.

      But remember, with great power comes great responsibility! In principle someone could enter some malicious code into B, although the int function mitigates this risk a little.

      Comment

      Working...