Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with calculator
#3
(May-02-2019, 10:59 AM)ichabod801 Wrote:   ###seperate operators and figures with space 
    for i in range(1,len(s)+count*2):   
        if (s[i] in operator)and(s[i+1] in operator) :
            s.insert(i+1," ")
            s.insert(i," ")
        elif (s[i] in operator)and(s[i-1] in num):
            s.insert(i+1," ")
            s.insert(i," ")
 
(May-02-2019, 10:59 AM)ichabod801 Wrote: I think the problem might be that you keep removing the results after inserting them. Take lines 67 and 68. You insert the result of the subtraction, and then immediately remove that same position. The first part of the program seems overly complicated. It looks like you want to make sure there are no spaces except there must be spaces around the operators (so the split works to make a list of numbers and operators). Wouldn't it be simpler to just use replace for each operator (replace('+', ' + '))? Also, I think slice assignment would work better than all that popping and inserting: s[s.index(i)-1:s.index(i)+2] = [v].

Hi, Thank you for the suggestion!
I do agree replace is faster, but, in that case, how could i avoid negative figures turn into ' - - ' ?
Also, I am stucked in line 16-23 seem not workable:
the loop can not work (create space) once it has been executed once.

Thank you!
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