Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question in Python code : Use dictionary/data structure to return more than one value from a function. So my code is fully operational, I just

Question in Python code: Use dictionary/data structure to return more than one value from a function. So my code is fully operational, I just need help in the portion where I need to return my results of the four operations in adictionaryAllInOne(num1,num2).

Sample output

res=AllInOne(5,2)

The results will be return in this format;

res is dictionary {\"add\":7, \"sub\":3, \"mult\":10, \"div\":2.5)

from res, you are going to print

5 + 2 = 7

5 - 2 = 3

5 * 2 = 10

5 / 2 = 2.5

res is dictionary {\"add\":7, \"sub\":3, \"mult\":10, \"div\":2.5)

NOTE:

The only thing I need in my main code is to condense this as a dictionary/data return structure and eliminate all of the print() lines.

elif choice == int('5'): #HELP_HERE_PLEASE#res = AllInOne(num1,num2) print(num1,\"+\",num2,\"=\", Mylib.scalc(str(num1)+','+str(num2)+',+')) print(num1,\"-\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',-')) print(num1,\"*\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',*')) print(num1, \"/\", num2, \"=\", round(Mylib.scalc(str(num1)+','+str(num2)+',/'), 12))

Main Code

#This is the start of the program import sys #This module holds my mathemathical calculations import Mylib def main(): # Takes input from the user, specified as an integer while True: try: #User prompt: user makes selection of what math arithmetic they want to use print(\"Select operation.\") print(\"1.Add 2.Subtract 3.Multiply 4.Divide 5.AllInOne\") choice = int(input(\"Enter choice(1/2/3/4/5): \")) #The ValueError was summoned and caught, so I informed the user about the error except ValueError: print(\"OOPS, you cause an error by not providing an integer.\") #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 high_range ): #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. \") break #Calculations made in regards with the math arithmetic selected if choice ==1: print(num1,\"+\",num2,\"=\", Mylib.scalc(str(num1)+','+str(num2)+',+')) elif choice == int('2'): print(num1,\"-\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',-')) elif choice == int('3'): print(num1,\"*\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',*')) elif choice == int('4'): try: num1 / num2 print(num1, \"/\", num2, \"=\", round(Mylib.scalc(str(num1)+','+str(num2)+',/'), 12)) #The ZeroDivisionError was summoned and caught, so I informed the user about the error except ZeroDivisionError: print(\"OOPS, you cause an error by dividing by zero. \") #Calculates all operations at once elif choice == int('5'): #HELP_HERE_PLEASE#res = AllInOne(num1,num2) print(num1,\"+\",num2,\"=\", Mylib.scalc(str(num1)+','+str(num2)+',+')) print(num1,\"-\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',-')) print(num1,\"*\",num2,\"=\",Mylib.scalc(str(num1)+','+str(num2)+',*')) print(num1, \"/\", num2, \"=\", round(Mylib.scalc(str(num1)+','+str(num2)+',/'), 12)) #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'): print() main() else: print(\"Thanks for using our calculator!\") break #Ends program sys.exit() main()

Import Functions #Mylib

# 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 # Function line: do all calculations at once def AllInOne(a,b): add = a + b sub = a - b mul = a * b div = a / b return {\"add\" : add, \"sub\" : sub, \"mult\" : mul, \"div\" : div}

#string processing function def scalc(p1): fstring=p1.split(\",\") if fstring[2]==\"*\": res= multiply(float(fstring[0]),float(fstring[1])) elif fstring[2]==\"+\": res= add(float(fstring[0]),float(fstring[1])) elif fstring[2]==\"-\": res= subtract(float(fstring[0]),float(fstring[1])) elif fstring[2]==\"/\": res= divide(float(fstring[0]),float(fstring[1])) return res def AllInOne(a,b): add = a + b sub = a - b mul = a * b div = a / b return {\"add\" : add, \"sub\" : sub, \"mult\" : mul, \"div\" : div}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

T F A retailer buys and sells merchandise.

Answered: 1 week ago

Question

T F Piggyback service is unique to air freight.

Answered: 1 week ago