Let s be a string that contains a simple mathematical expression, e.g.,
s = '1.5 + 2.1-3'
s = '10.0-1.6 + 3 - 1.4'
The expression will have multiple operands. We don't know exactly how many operands there are in s.
The operator will be one of the following: +, -.
Write a function called plus_minus() that takes s as the input, interpret the expression, then evaluate and return the output.
Note: The use of the eval() is not allowed in this exercise. Even if you do pass all the test cases, if you use eval() in your code, you will receive a grade of 0 for this question!
My codes below here:
def plus_minus(s):
# enter your code below this line
num_list = []
operator = []
temp = ''
for i in s:
if i not in ['+', '-']:
temp = temp + i
else:
num_list.append(temp)
temp = ''
operator.append(i)
num_list.append(temp)
result=float(num_list[0])
for i in range(0,len(operator)):
if operator[i]=='+':
result+=float(num_list[i+1])
elif operator[i]=='-':
result-=float(num_list[i+1])
return result
It works when s = '1.5 + 2.1-3' and s = '10.0-1.6 + 3 - 1.4', but it doesn't work when s='-1.5+2.1-3'
I have solved the problem please go through the comments.
First collecting numbers and operators differently is first later we will convert the number list strings to float values.
The case when '-' comes first should be handled separately and in any case if somebody enter '+' at start that case as well is handled.
def plus_minus(s):
# enter your code below this line
num_list = [] # list for collecting numbers
operator = [] # list for collecting operators
temp = '' # empty string
for i in s: # looping for string
# till + or - are seen we combine each number to a
# single string like so that 1.5 200 numbers form a single string
if i not in ['+', '-']:
temp = temp + i
else: # when + or - are seen we add the exisiting temp string to numbers list and make the string empty
num_list.append(temp.strip())
temp = ''
operator.append(i)
num_list.append(temp) # the final number is attached not attached to num_list becase for loops ends before appending
# print(operator)
# print(num_list)
if num_list[0] == '': # this happend only when string starts with a '-' or '+' operator
num_list.pop(0) # we pop the first empty space in number list because of '-' or '+'
# if the operator is - we multiply -1 with the first number and pop the first operator is opertor list as well
if operator[0] == '-':
operator.pop(0)
num_list[0] = -1 * float(num_list[0])
else:
operator.pop(0)
num_list[0] = float(num_list[0])
for i in range(1,len(num_list)): # making rest of the elements of list to float objects
num_list[i] = float(num_list[i])
# if there is no '' empty string at starting of num list there is no operator at
# starting so convert all the elements to float without any problem
else:
for i in range(len(num_list)):
num_list[i] = float(num_list[i])
# print(num_list)
result = num_list[0] # result will be initiated with numlist[0] value and add the rest as per the operators
for i in range(len(operator)):
if operator[i] == '+':
result += num_list[i+1]
if operator[i] == '-':
result -= num_list[i+1]
return result
plus_minus('-5+2+6-8')
Image Snippets of code and output:
Output for different Examples:
If you want to round the 2 decimal points just write round(result,2) in the code in place result while returning from the function.
Drop a comment for any further doubts.
Get Answers For Free
Most questions answered within 1 hours.