Question: I'm stuck in a python program where I need to add a string function to the calculator program that I already have. The subject that

I'm stuck in a python program where I need to add a string function to the calculator program that I already have. The subject that I'm in is "Modules and Strings". I understand the concept of creating another file and importing the module into the main code that I'm working with. I just haven't been able to figure out how to use string functions to parse the first user input number, the second user input number, and the operator from the input string.A t the beginning I'll be giving the specifications in what needs to be done. I'm posting the functioning code that I have and the "Mylib" file module. At the end I'll also be giving an example I found that may be useful but still can't figure it out.

Modules and Strings / Question description

Add the following function into Mylib

scalc(p1)

p1 will be a string like this "N1, N2, operator"

examples

scalc("20,30,*")

the result will be 600

scalc("50,20,+")

the result will be 70

scalc("50,20,-")

the result will be 30

scalc("60,20,/")

the result will be 30

use string functions to parse the first number, the second number, and the operator from the input string.

use the prior functions (add, subtract, divide and multiply ) to do the calculations

My Main Code #This is the start of the program import sys #This module holds my mathemathical calculations import Mylib def main(): #User prompt: user makes selection of what math arithmetic they want to use print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") # Takes input from the user, specified as an integer while True: try: choice = int(input("Enter choice(1/2/3/4): ")) break #The ValueError was summoned and caught, so I infomed the user about the error except ValueError: print("OOPS, you cause an error by not providing an integer.") print("Thanks for using our calculator!") #This commands the program to exit sys.exit() #User prompt: asks about input of a range of values the user can create print("Please input a range you want your values to be in-between.") # Takes inputs from the user low_range = float(input("Enter your lower range: ")) high_range = float(input("Enter your high range: ")) num1 = float(input("Enter your first number: ")) num2 = float(input("Enter your second number: ")) #Loop that assures the user entered the input values between the range previously selected while True: if (num1 >= low_range and num2 <= high_range): pass else: #Message that they elected values out of range selected print("The input values are out side the input ranges. " "Please check the numbers and try again. " "Thanks for using our calculator!") break #Calculations made in regards with the math arithmetic selected if choice == int('1'): print(num1,"+",num2,"=", Mylib.add(num1,num2)) print("Thanks for using our calculator!") elif choice == int('2'): print(num1,"-",num2,"=", Mylib.subtract(num1,num2)) print("Thanks for using our calculator!") elif choice == int('3'): print(num1,"*",num2,"=", Mylib.multiply(num1,num2)) print("Thanks for using our calculator!") elif choice == int('4'): try: num1 / num2 print(num1, "/", num2, "=", round(Mylib.divide(num1,num2), 12)) print("Thanks for using our calculator!") #The ZeroDivisionError was summoned and caught, so I infomed the user about the error except ZeroDivisionError: print("OOPS, you cause an error by dividing by zero. ") print("Thanks for using our calculator!") #Loop: asking user if they want to continue with another calculation while True: prompt = (input("Do you want another go (Y/N)?: ")) #User can elect decision from these selections if (prompt == 'Y' or prompt == 'y'): main() else: print("Thanks for using our calculator!") break #Ends program break main()

Mylib file module that holds my calculations

# Function line: adds two numbers def add(x, y): return x + y # Function line: subtracts two numbers def subtract(x, y): return x - y # Function line: multiplies two numbers def multiply(x, y): return x * y # Function line: divides two numbers def divide(x, y): return x / y

Code that I found that may be useful to solve my issue.

def calc(p):

fstring=p.split(",")

if fstring[2]=="*":

res= int(fstring[0]) * int(fstring[1])

if fstring[2]=="+":

res= int(fstring[0]) + int(fstring[1])

return res astring = "5,10,+"

print(calc(astring))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!