1

I have a string variable called areacalc. The string inside it reads 58 * 45.

How do i get the answer out of this string? I've tried area_sum = float(areacalc) and area_sum = areacalc but it doesn't provide the sum answer on either occasion, it just thinks that I want area_sum to be the same as the string itself, rather than the answer.

areacalc = str(58 * 45)
area_sum = float(areacalc)

I want area_sum to be the answer of 58 * 45 (which is 2610)

  • 1
    Can you share some code? I think this question will be easier to understand with an example of what you're trying to do. [Edit: What you're trying to do is probably straightforward and will not require more than a few lines of code. And someone will eventually suggest `eval` but this is not considered good practice.] – Matt Hall Jul 27 '21 at 14:52
  • This should be easy to solve if you post what code you already have. – Dan Curry Jul 27 '21 at 14:52
  • 1
    `eval(areacalc)` will evaluate this string as python. – rob Jul 27 '21 at 14:53
  • 1
    `eval` will also execute arbitrary python and do whatever it says, and is a terrible suggestion here, especially if `areacalc` is user provided. – Donnie Jul 27 '21 at 14:54
  • if OP is trying to get the sum of the values 58 and 45 from the string, eval() will not accomplish this as is. – Dan Curry Jul 27 '21 at 14:54
  • @rob yes that will work, one thing to add, is that leading zeros in decimal integer literals are not permitted, which means that 45*05 will raise an error – oubaydos Jul 27 '21 at 14:54
  • I don't know why this was closed — the linked answer is a horrible way for this user to solve this problem. @cabana If you ask your question with your code, we can try to help you again. – Matt Hall Jul 27 '21 at 14:55
  • 4
    @oubaydos I'd usually not suggest ever using eval() as it's typically buggy and unsafe if not implemented carefully. However this question doesn't have any code or use cases and only seeks to resolve a specific string case. – rob Jul 27 '21 at 15:03
  • @kwinkunks Thanks, i've added the code, although it really is that basic. Im just looking to get the area_sum to hold the value of the answer to the calculation that's inside the string areacalc. –  Jul 27 '21 at 15:08
  • @kwinkunks there are several answers given at the duplicate, taking varying approaches, and the top voted one is not bad - it is the right idea for the problem in general (and we have no idea how general OP needs it to be *nor why the problem needs to be solved at all*). Just because it isn't as simple as one might like, isn't a problem. After all, Python itself has to do far more work than that for the analogous task. – Karl Knechtel Jul 27 '21 at 15:10
  • @KarlKnechtel It is a problem is the goal is to help this particular user. As you can infer from the question they are asking, this user is likely a beginner. Closing this question was the wrong move here. – Matt Hall Jul 27 '21 at 15:25
  • @Cabana Is there a reason why you can't do `area_sum = 58 * 45`? If you already have those two numbers as numbers (i.e. you are not getting them as a string from somewhere else), then you can multiply them directly. You don't need to cast them to a float — `58 * 45` is already a number. – Matt Hall Jul 27 '21 at 15:27
  • All the more reason for the question to be closed, really. It demonstrates numerous conceptual misunderstandings and Stack Overflow is *not a discussion forum*. OP should follow a tutorial, and direct questions about the tutorial material to somewhere like Reddit or Quora. The fact that OP wants to solve a problem that *looks* simple but is *fundamentally out of beginner scope* is not Stack Overflow's problem. – Karl Knechtel Jul 27 '21 at 15:29
  • @KarlKnechtel I and others were asking questions of the OP to clarify and add context, **to try to help them**. The OP clearly has not used the platform much. They need help and that is, I believe, the purpose of this site. Posting a solution using parsing is obviously not the shortest path to success here. – Matt Hall Jul 27 '21 at 15:32
  • 1
    "Posting a solution using parsing" is the *only corrrect* path to success *with the problem actually asked about*. If we have to guess about a *different* problem, then that is *still a reason to close the question*. – Karl Knechtel Jul 27 '21 at 15:33
  • @kwinkunks I can't unfortunately, as the data of area_sum changes depending on which iteration of a larger loop it is in. I simplified the code down greatly as posting the entire chunk in is pointless and won't make much sense without the bit before and so forth, i would end up posting 500 lines of code. Please can you both stop arguing, i was looking for a way other than eval(), but i will just use eval() and convert it back to a string and slice out the first 6 or so indexes to shake off the decimal places. Thankyou for the help regardless Kwinkunks, it's much appreciated. –  Jul 27 '21 at 15:35
  • @KarlKnechtel I have to say i thought the point of this website was to help others solve problems and learn new solutions to mistakes. I honestly thought it was just a simple question that didn't require a huge context or a massive explanation, and was just something that slipped under my radar. Why would i look for an entire tutorial for such a basic issue, is this website not intended to solve these issues, rather than providing an entire teaching course..? –  Jul 27 '21 at 15:39
  • Yes, the point of the website is to solve problems, which is why there's a link to the duplicate for the problem that needs solving. It is not for teaching the fundamentals of the language. I appreciate that you thought it was a simple question, but some things that look simple just aren't. I am not telling you to look for an entire tutorial "for such a basic issue"; tutorials normally don't exist for any particular "issue". I am telling you to look for a tutorial to *learn the fundamentals of Python programming* because *that is what you need at this point*. – Karl Knechtel Jul 27 '21 at 15:43
  • It is the same as if you were learning to do *anything else*. If you were trying to solve a chess puzzle and didn't properly understand how the pieces move, I would tell you to follow a tutorial on the rules of chess first, and probably then a tutorial on basic tactics. – Karl Knechtel Jul 27 '21 at 15:45

0 Answers0