How to make a String an Int [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MaxLindquist
    New Member
    • Oct 2006
    • 24

    How to make a String an Int [solved]

    Hi! My problem is that im trying to make an Int out of the string a=X+Y*Z-A/B... (for example 1-2+6-3*2/3-3). when i do Int(a) it doesn't work beacuse of the (+,-,/,*)

    What should i do?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by MaxLindquist
    Hi! My problem is that im trying to make an Int out of the string a=X+Y*Z-A/B... (for example 1-2+6-3*2/3-3). when i do Int(a) it doesn't work beacuse of the (+,-,/,*)

    What should i do?
    Would something like this work for you?
    Code:
    x,y,z,w = 3.0,12,16,4.5
    a='x+y*z-w/x'
    print eval(a)
    print int(eval(a))

    Comment

    • MaxLindquist
      New Member
      • Oct 2006
      • 24

      #3
      Yes... thank you very much

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bvdet
        Would something like this work for you?
        Code:
        x,y,z,w = 3.0,12,16,4.5
        a='x+y*z-w/x'
        print eval(a)
        print int(eval(a))
        or get the floating point (since they're in the formula) by:

        Code:
        print eval(a)

        Comment

        • MaxLindquist
          New Member
          • Oct 2006
          • 24

          #5
          What if i want the calculation to skip the usual rules, so that x+y*z-a/b is like ((((x+y)*z)-a)/b)?

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by MaxLindquist
            What if i want the calculation to skip the usual rules, so that x+y*z-a/b is like ((((x+y)*z)-a)/b)?
            Use the parens in the string or break it up and eval pieces at a time.
            Sounds like you're working on some sort of language parser...
            What's the project about? One of my early projects was an assembler for moto micros. Keep posting,
            Barton

            Comment

            • MaxLindquist
              New Member
              • Oct 2006
              • 24

              #7
              Im making a dicegame. I'm an absoulte beginner and i am not very good with computers, so a am very thankful for your help!

              Comment

              Working...