Question: when I try to enter an expression like 2+-2 I get an answer of 4 when it should be 0. I combined the list into
when I try to enter an expression like 2+-2 I get an answer of 4 when it should be 0. I combined the list into a single string and set a condition for it in my subtraction if statement. I still get 4, I do not understand, please help!
In [45]: import re ##This is a function that does not take in any parameters at all ##We also won't be returning anything def calculator(): user_string - input("Please enter a mathematical operation you wish to perfo ##Write your code here nums = re.findall(r'\d+', user_string) print(nums) ex = re.findall(r'd', user_string) print(ex) nums =list(map(int, nums)) ex = ".join(ex) print(ex) if '+' in ex: ans = nums[@] + nums[1] elif '-' or '+-' in ex: ans = nums[@] - nums[1] elif '/' in ex: ans = nums[0] / nums [1] elif 'x' in ex: ans = nums[@] * nums[1] elif '^' in ex: ans = nums[@] ** nums [1] elif 'm' in ex: ans = nums[@] % nums[1] else: print("Your entry of " +user_string+ " is not valid.") print(ans) ## print the result, do not return it In [46]: calculator) Please enter a mathematical operation you wish to perform: 2+-2 ['2', '2'] ['+', '-'] +
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
