URGENT HELP on calculator code pyhton


#1

hello there,

this is a simple deconstructed calculator i made, but i made a mistake somewhere i cant figure it out, please help.

def split(string):
list = [] #create an empty list for the loop to add numbers and operators in each index
blank = ''
for i in range(len(string)):
if string[i] == ‘+’ or string[i] == ‘-’ or string[i] == ‘’ or string[i] == ‘/’ or string[i] ==’%’:
if string[i+1] == '
’ or string[i+1] == ‘/’: #In case the operator is ** or //
combine = string[i] +string[i+1] #put it in the same string in the same index
list.append(blank) #add to the list
list.append(combine)
blank = ‘’ #reasigning blank so it can do the job again without adding the previous variable in the second index
else:
if string[i-1] == ‘’ or string[i-1] == ‘/’:
continue
else:
list.append(blank)
list.append(string[i])
blank = ‘’ #reasigning the blank again
elif 48 <= ord(string[i]) <= 57: #If the loop runs through a number, put them in an index
blank = blank+string[i]
if i==len(string) - 1:
list.append(blank)
return list
def calculation(string): #Function for calculating
i = 0
list = split(string)
while i <len(list): #Create a loop that runs through the list
if list[i] == ‘+’: #if it runs through an index that has '+'
calculate = int(float(list[i-1])) + int(float(list[i+1])) #the plus sign is i, so we use the index before and after the plus sign -which are numbers, add them together and put it in a storage
del list[i+1] #delete the two indexs that has numbers
del list[i-1]
list[i-1] = str(calculate) #if we delete [i-1], it means [i] is now [i-1], reasign it with the result in the storage, turn it into string
i = 0
if list[i] == ‘-’:
calculate = int(float(list[i-1])) - int(float(list[i+1]))
del list[i+1]
del list[i-1]
list[i-1] = str(calculate)
i = 0 #the same for other operations
if list[i] == '
’:
calculate = int(float(list[i-1])) * int(float(list[i+1]))
del list[i+1]
del list[i-i]
list[i-1] = str(calculate)
i = 0
if list[i] == ‘/’:
calculate = int(float(list[i-1])) / int(float(list[i+1]))
del list[i+1]
del list[i-i]
list[i-1] = str(calculate)
i = 0
if list[i] == ‘**’:
calculate = int(float(list[i-1])) ** int(float(list[i+1]))
del list[i+1]
del list[i-i]
list[i-1] = str(calculate)
i = 0
if list[i] == ‘//’:
calculate = int(float(list[i-1])) // int(float(list[i+1]))
del list[i+1]
del list[i-i]
list[i-1] = str(calculate)
i = 0
if list[i] == ‘%’:
calculate = int(float(list[i-1])) % int(float(list[i+1]))
del list[i+1]
del list[i-1]
list[i-1] = str(calculate)
i = 0
i = i +1
return int(float(list[0]))
print('Warning! This program does not calcucate operations in BODMAS. ')
x = str(input(‘Type in the operation you want to calculate:’)) #input for the calculation
for c in x:
if (97 <= ord© <= 122) or (65 <= ord© <= 90): #If one or more chacracters in the string are upercase or lowercase characters
print(‘Your input should be an operator.’) #Give a warning
break
else:
print(calculation(x)) #If it is an operator, do the calculation
break


#2

You say urgent, yet you’ve left your code all mangled and haven’t explained the problem. Can’t be very urgent for you since you show no interest in making it easy to help you. You must mean that it is urgent for others to answer you, that you somehow prioritize the time of others.


#3

So far I found an incomplete if statement on line 40 related to the multiplier case and others that you can check on the code comments. Also don’t use list as name identifier since it is reserved for the list datatype, so I renamed it to list_1.

At last (probably a formatting code when you uploaded your issue), there is some issue with the last code block, so I fixed it, and it works now! :sparkler:

100% Fixed code

def split(string):
  list_1 = [] #create an empty list_1 for the loop to add numbers and operators in each index
  blank = ''
  for i in range(len(string)):
    if string[i] == '+' or string[i] == '-' or string[i] == '*' or string[i] == '/' or string[i] =='%':
      # Mistake below
      if string[i+1] == '*' or string[i+1] == '/': #In case the operator is ** or //
        combine = string[i] + string[i+1] # Put it in the same string in the same index
        list_1.append(blank) #add to the list_1
        list_1.append(combine)
        blank = '' #reasigning blank so it can do the job again without adding the previous variable in the second index
      else:
        # Mistake below too
        if string[i-1] == '*' or string[i-1] == '/':
          continue
        else:
          list_1.append(blank)
          list_1.append(string[i])
          blank = '' #reasigning the blank again
    elif 48 <= ord(string[i]) <= 57: #If the loop runs through a number, put them in an index
      blank = blank+string[i]
      if i==len(string) - 1:
        list_1.append(blank)
  return list_1
        
def calculation(string): #Function for calculating
  i = 0
  list_1 = split(string)
  while i <len(list_1): #Create a loop that runs through the list_1
    if list_1[i] == '+': #if it runs through an index that has '+'
      calculate = int(float(list_1[i-1])) + int(float(list_1[i+1])) #the plus sign is i, so we use the index before and after the plus sign -which are numbers, add them together and put it in a storage
      del list_1[i+1] #delete the two indexs that has numbers
      del list_1[i-1]
      list_1[i-1] = str(calculate) #if we delete [i-1], it means [i] is now [i-1], reasign it with the result in the storage, turn it into string
      i = 0
    if list_1[i] == '-':
      calculate = int(float(list_1[i-1])) - int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1]
      list_1[i-1] = str(calculate)
      i = 0 #the same for other operations
    if list_1[i] == '*': # You had a mistake here :P
      calculate = int(float(list_1[i-1])) * int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1] # Mistake here too!
      list_1[i-1] = str(calculate)
      i = 0
    if list_1[i] == '/':
      calculate = int(float(list_1[i-1])) / int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1]
      list_1[i-1] = str(calculate)
      i = 0
    if list_1[i] == '**':
      calculate = int(float(list_1[i-1])) ** int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1]
      list_1[i-1] = str(calculate)
      i = 0
    if list_1[i] == '//':
      calculate = int(float(list_1[i-1])) // int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1]
      list_1[i-1] = str(calculate)
      i = 0
    if list_1[i] == '%':
      calculate = int(float(list_1[i-1])) % int(float(list_1[i+1]))
      del list_1[i+1]
      del list_1[i-1]
      list_1[i-1] = str(calculate)
      i = 0
    i = i +1
  return int(float(list_1[0]))

print('Warning! This program does not calculate operations in BODMAS. ')
  
x = str(input('Type in the operation you want to calculate:')) #input for the calculation
for c in x:
  if (97 <= ord(c) <= 122) or (65 <= ord(c) <= 90): #If one or more characters in the string are upercase or lowercase characters
    print('Your input should be an operator.') #Give a warning
    break
  else:
    print(calculation(x)) #If it is an operator, do the calculation
    break

Next time, take a look at this in order to post code and save us the time to re-write it. Keep learning and learn from mistakes :smiley:


@ionatan Sorry if I fixed the whole code before making him aware of how to properly format his code. I thought he needed a hero as soon as possible.


#4

Not at all, you’re welcome to respond however you want.


#5

hello mr.salty(@ionatan) ,

being a complete noob to programming in general, let alone programming in python; i legit started learning to code 3 days ago, i couldn’t properly elaborate what was wrong was because i myself didnt know what was wrong. again, returning back to the fact im a noob, i wasn’t aware of the proper layout on how to ask for help. which i now do, thats on me for not reading into this more. However, for future reference, maybe ask the person to submit their code in a more understandable way if they haven’t instead of assuming they are prioritizing the time of others.

Thanks:slight_smile:


#6

You should already know enough to be able to look at your post and see that it can’t be copied to get the same result.

You should also be capable of explaining what is happening differently from what you expect, otherwise you wouldn’t be asking.

Imagine walking up to someone on the street, saying I URGENTLY NEED THIS SOLVED and dropping a stack of paper on the ground in a mess.

When you ask something of a stranger you need to take care that the question is easily understood, that there’s minimal amount of effort needed to answer, and that they can see that you’re not able to do it yourself, that the question is worth answering.


#7

hello Hero(@kingdarboja) ,

i apologize for not formatting my code in to a suitable form for my code to be easier understood. This was poor planning on my part but either way i truly do appreciate the help/fixing of my code. not only that, but appreciate you attaching a link so that i may be able to follow a proper guideline for the next time i may require help. thanks again
have a great night :smile:


#8

I do have some sympathy for that just about everything is confusing at the moment. But I think the walking-up-to-a-stranger-on-the-street analogy is enough for you to write perfectly fine questions.


#9

i understand that now, which is why i said it would have been easier to have told me what i did wrong, instead of assuming that my sense of urgency was a medium to prioritize others time .


#10

p.s- as much as it pains me to say this, but your walking-up-to-a-stranger-on-the-street analogy is truly a work of art.


#12

Maybe you fixed it since you withdrew that post, but index-out-of-bounds should be pretty easy to figure out by adding prints for the index used and the list/listsize and maybe some information of which index you meant to use (basically having a look at what’s happening, comparing to how you would have done it yourself manually)
And yeah, they probably fixed just what issues were caused by the formatting, not knowing what the problem was.


#13

Hi @microwhiz98563,

See Quick Tips for Writing Good Posts. Among other things, it provides information on formatting code when posting it.

@kingdarboja, thanks for the formatting. When a new user does not know how to post code properly, we can send that user there for advice regarding how it should be done.