Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with calculator
#4
You don't need to avoid negative figures turning into double operators. Let that happen, and then use replace on any double operators: text = text.replace(' - - ', ' - -'). But that may get tricky with double spaces cropping up.

I would normally approach this with regular expressions. Assuming this is homework and you can't use regular expressions, I would do one loop over the input string. For each character, if it's a number character, store it in a sub-string and build that sub-string character by character. When you get to an operator, append the number and then the operator into your list, and reset the sub-string. If you get an operator after an operator (or an operator at the beginning) you know that's a negative number, and you can put that in the sub string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Problems with calculator - by muachihiyo - May-02-2019, 09:00 AM
RE: Problems with calculator - by ichabod801 - May-02-2019, 10:59 AM
RE: Problems with calculator - by muachihiyo - May-02-2019, 03:45 PM
RE: Problems with calculator - by ichabod801 - May-02-2019, 06:12 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020