Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

TOPIC: Python coding: Lists and other data structures 2 Questions 1) Use list to create a menu 2) Create a function the will return the

TOPIC: Python coding: Lists and other data structures

2 Questions

1) Use list to create a menu

2) Create a function the will return the results of the four operations in a dictionary allInOne(n1,n2)

Sample output

1) Add two numbers

2) Mult two number

3) Divide

4) Scalc

5) all in one ..

6)

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

NOTE: I'm a mess with what I have been coding. I posted my questions above and the code I have so far right now.

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") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = int(input("Enter choice(1/2/3/4): ")) #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 < low_range or num2 > 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. ") #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()

Mylib imported code

# 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 #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

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

Step: 3

blur-text-image

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

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

=+Which associations exist?

Answered: 1 week ago