SlideShare a Scribd company logo
1 of 7
Mylib.py
def allInOne(n1, n2):
return {"add":plus(n1, n2),
"sub":subtract(n1, n2),
"mult":multiply(n1, n2),
"div": divide(n1, n2)}
def IsInRange(lr, hr, n):
return lr < n and n < hr
#add 2 numbers
def plus(n1,n2):
add = n1 + n2
return add
#subtract 2 numbers
def subtract(n1, n2):
sub = n1 - n2
return sub
#multiply 2 numbers
def multiply(n1, n2):
mult = n1 * n2
return mult
#division, n2 should not be zero
def divide(n1, n2):
try:
div = n1/(n2 + 0.0)
return div
except ZeroDivisionError as e:
print("The result of %s/%s = %s" % (n1, n2,"You cannot
divide by Zero"))
except Exception :
print("Invalid input" , n1, n2)
#expression calculator
def scalc(val):
try:
#the epression is assumed to be comma separated
ns = val.split(",")
#split it into 3 parts
#remove whitespaces in the components
n1 = int(ns[0].strip())
n2 = int(ns[1].strip())
op = ns[2].strip()
#check if operator is one of the supported
if op == "*":
return multiply(n1, n2)
elif op == "/":
return divide(n1, n2)
elif op == "+":
return plus(n1, n2)
elif op == "-":
return subtract(n1, n2)
else:
print("Unknown operator", op)
#in case of invalid input catch the error
except Exception :
print("Invalid input:" , val)
W6_first_last.py
# Program name :
# Student Name :
# Course : ENTD220
# Instructor :
# Date : 02/12/2020
# Description : Expression & Arithmetic Calculator
# Copy Wrong : This is my work
import Mylib
def doScalc():
print("---------Expression calculator------------------")
print("Enter the expression with format N1,N2,operator:")
print("Supported operator * , /, -, +")
print("E.g 5, 4, *)")
val = input()
res = Mylib.scalc(val)
print('The result of "' + val + '" is', res)
def showList():
print("1) Add two numbers")
print("2) Multiply two numbers")
print("3) Subtract two numbers")
print("4) Divide two numbers")
print("5) Scalc")
print("6) All in one")
print("7) Exit")
def getUserInput():
low = int(input("Enter Lower range:"))
high = int(input("Enter Higher range:"))
n1 = int(input("Enter first number:"))
n2 = int(input("Enter second number:"))
return [low, high, n1 , n2]
def doCalculation(op):
user = getUserInput()
low = user[0]
high = user[1]
n1 = user[2]
n2 = user[3]
if Mylib.IsInRange(low , high, n1) and 
Mylib.IsInRange(low , high, n2):
if op == "+":
res = Mylib.plus(n1, n2)
elif op == "*":
res = Mylib.multiply(n1, n2)
elif op == "-":
res = Mylib.subtract(n1, n2)
elif op == "/":
res = Mylib.divide(n1, n2)
print("%d %s %d = %d" % (n1, op, n2, res))
else:
print("Error: input out of range")
def doAllInOne():
user = getUserInput()
low = user[0]
high = user[1]
n1 = user[2]
n2 = user[3]
if Mylib.IsInRange(low , high, n1) and 
Mylib.IsInRange(low , high, n2):
mydict = Mylib.allInOne(n1, n2)
print("%d + %d = %d" % (n1, n2, mydict["add"]))
print("%d - %d = %d" % (n1, n2, mydict["sub"]))
print("%d * %d = %d" % (n1, n2, mydict["mult"]))
print("%d / %d = %d" % (n1, n2, mydict["div"]))
def main():
while True:
showList()
action = int(input("What do you want to do?"))
if action == 7:
break;
if action == 1:
user = getUserInput
doCalculation("+")
elif action == 2:
doCalculation("*")
elif action == 3:
doCalculation("-")
elif action == 4:
doCalculation("/")
elif action == 5:
doScalc()
elif action == 6:
doAllInOne()
print()
main()
print("Thanks for using our calculator!")

More Related Content

Similar to Mylib.pydef allInOne(n1, n2) return {addplus(n1, n2).docx

Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
kesav24
 
C programs
C programsC programs
C programs
Minu S
 

Similar to Mylib.pydef allInOne(n1, n2) return {addplus(n1, n2).docx (20)

Practicle 1.docx
Practicle 1.docxPracticle 1.docx
Practicle 1.docx
 
Basic python programs
Basic python programsBasic python programs
Basic python programs
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
Practical File waale code.pdf
Practical File waale code.pdfPractical File waale code.pdf
Practical File waale code.pdf
 
Programs.doc
Programs.docPrograms.doc
Programs.doc
 
AI-Programs.pdf
AI-Programs.pdfAI-Programs.pdf
AI-Programs.pdf
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdf
 
Matlab project
Matlab projectMatlab project
Matlab project
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
xii cs practicals
xii cs practicalsxii cs practicals
xii cs practicals
 
hotelmangt project code connecting python wih Mysql cbse 2022-23 project .docx
hotelmangt project code connecting python wih Mysql cbse 2022-23 project .docxhotelmangt project code connecting python wih Mysql cbse 2022-23 project .docx
hotelmangt project code connecting python wih Mysql cbse 2022-23 project .docx
 
DSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxDSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docx
 
Retos de Programación en Python
Retos de Programación en PythonRetos de Programación en Python
Retos de Programación en Python
 
C programs
C programsC programs
C programs
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 

More from roushhsiu

Moral rights as opposed to legal rights are not dependent on a polit.docx
Moral rights as opposed to legal rights are not dependent on a polit.docxMoral rights as opposed to legal rights are not dependent on a polit.docx
Moral rights as opposed to legal rights are not dependent on a polit.docx
roushhsiu
 
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docxMontasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
roushhsiu
 
Module Outcome  You will be able to describe the historical force.docx
Module Outcome  You will be able to describe the historical force.docxModule Outcome  You will be able to describe the historical force.docx
Module Outcome  You will be able to describe the historical force.docx
roushhsiu
 
Module One Making Budgetary DecisionsDirectionsBased on the i.docx
Module One Making Budgetary DecisionsDirectionsBased on the i.docxModule One Making Budgetary DecisionsDirectionsBased on the i.docx
Module One Making Budgetary DecisionsDirectionsBased on the i.docx
roushhsiu
 
Monitoring Global Supply Chains† Jodi L. Short Prof.docx
Monitoring Global Supply Chains†   Jodi L. Short Prof.docxMonitoring Global Supply Chains†   Jodi L. Short Prof.docx
Monitoring Global Supply Chains† Jodi L. Short Prof.docx
roushhsiu
 
Morality Relativism & the Concerns it RaisesI want to g.docx
Morality Relativism & the Concerns it RaisesI want to g.docxMorality Relativism & the Concerns it RaisesI want to g.docx
Morality Relativism & the Concerns it RaisesI want to g.docx
roushhsiu
 
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docxModule Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
roushhsiu
 
Module Assignment Clinical Decision Support SystemsLearning Outcome.docx
Module Assignment Clinical Decision Support SystemsLearning Outcome.docxModule Assignment Clinical Decision Support SystemsLearning Outcome.docx
Module Assignment Clinical Decision Support SystemsLearning Outcome.docx
roushhsiu
 
MONTCLAIR UNIVERSITYLAWS 362 LEGAL WRITING MIDTERM.docx
MONTCLAIR UNIVERSITYLAWS 362  LEGAL WRITING   MIDTERM.docxMONTCLAIR UNIVERSITYLAWS 362  LEGAL WRITING   MIDTERM.docx
MONTCLAIR UNIVERSITYLAWS 362 LEGAL WRITING MIDTERM.docx
roushhsiu
 
More like this Abstract TranslateFull Text Translate.docx
More like this Abstract TranslateFull Text Translate.docxMore like this Abstract TranslateFull Text Translate.docx
More like this Abstract TranslateFull Text Translate.docx
roushhsiu
 

More from roushhsiu (20)

Most women experience their closest friendships with those of th.docx
Most women experience their closest friendships with those of th.docxMost women experience their closest friendships with those of th.docx
Most women experience their closest friendships with those of th.docx
 
Morgan and Dunn JD have hired you to assist with a case involvin.docx
Morgan and Dunn JD have hired you to assist with a case involvin.docxMorgan and Dunn JD have hired you to assist with a case involvin.docx
Morgan and Dunn JD have hired you to assist with a case involvin.docx
 
Mortality rates vary between the Hispanic community and the gene.docx
Mortality rates vary between the Hispanic community and the gene.docxMortality rates vary between the Hispanic community and the gene.docx
Mortality rates vary between the Hispanic community and the gene.docx
 
Moreno Industries has adopted the following production budget for th.docx
Moreno Industries has adopted the following production budget for th.docxMoreno Industries has adopted the following production budget for th.docx
Moreno Industries has adopted the following production budget for th.docx
 
Most people have a blend of leadership styles that they use. Some le.docx
Most people have a blend of leadership styles that they use. Some le.docxMost people have a blend of leadership styles that they use. Some le.docx
Most people have a blend of leadership styles that they use. Some le.docx
 
Moral rights as opposed to legal rights are not dependent on a polit.docx
Moral rights as opposed to legal rights are not dependent on a polit.docxMoral rights as opposed to legal rights are not dependent on a polit.docx
Moral rights as opposed to legal rights are not dependent on a polit.docx
 
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docxMontasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
Montasari, R., & Hill, R. (2019). Next-Generation Digital Forens.docx
 
Module Outcome  You will be able to describe the historical force.docx
Module Outcome  You will be able to describe the historical force.docxModule Outcome  You will be able to describe the historical force.docx
Module Outcome  You will be able to describe the historical force.docx
 
Molière believed that the duty of comedy is to correct human vices b.docx
Molière believed that the duty of comedy is to correct human vices b.docxMolière believed that the duty of comedy is to correct human vices b.docx
Molière believed that the duty of comedy is to correct human vices b.docx
 
Module One Making Budgetary DecisionsDirectionsBased on the i.docx
Module One Making Budgetary DecisionsDirectionsBased on the i.docxModule One Making Budgetary DecisionsDirectionsBased on the i.docx
Module One Making Budgetary DecisionsDirectionsBased on the i.docx
 
Monitoring Data and Quality ImprovementAnswer one of two que.docx
Monitoring Data and Quality ImprovementAnswer one of two que.docxMonitoring Data and Quality ImprovementAnswer one of two que.docx
Monitoring Data and Quality ImprovementAnswer one of two que.docx
 
Monitoring Global Supply Chains† Jodi L. Short Prof.docx
Monitoring Global Supply Chains†   Jodi L. Short Prof.docxMonitoring Global Supply Chains†   Jodi L. Short Prof.docx
Monitoring Global Supply Chains† Jodi L. Short Prof.docx
 
Morality Relativism & the Concerns it RaisesI want to g.docx
Morality Relativism & the Concerns it RaisesI want to g.docxMorality Relativism & the Concerns it RaisesI want to g.docx
Morality Relativism & the Concerns it RaisesI want to g.docx
 
Module 9 content You will perform a history of a cardiac pro.docx
Module 9 content You will perform a history of a cardiac pro.docxModule 9 content You will perform a history of a cardiac pro.docx
Module 9 content You will perform a history of a cardiac pro.docx
 
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docxModule Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
Module Assessment 4 TANM ApplicationsBUS2 190Last name, Fir.docx
 
Module Assignment Clinical Decision Support SystemsLearning Outcome.docx
Module Assignment Clinical Decision Support SystemsLearning Outcome.docxModule Assignment Clinical Decision Support SystemsLearning Outcome.docx
Module Assignment Clinical Decision Support SystemsLearning Outcome.docx
 
MONTCLAIR UNIVERSITYLAWS 362 LEGAL WRITING MIDTERM.docx
MONTCLAIR UNIVERSITYLAWS 362  LEGAL WRITING   MIDTERM.docxMONTCLAIR UNIVERSITYLAWS 362  LEGAL WRITING   MIDTERM.docx
MONTCLAIR UNIVERSITYLAWS 362 LEGAL WRITING MIDTERM.docx
 
MODULE 8You will perform a history of a respiratory problem th.docx
MODULE 8You will perform a history of a respiratory problem th.docxMODULE 8You will perform a history of a respiratory problem th.docx
MODULE 8You will perform a history of a respiratory problem th.docx
 
Most organizations, including hospitals, adopt both Mission and Visi.docx
Most organizations, including hospitals, adopt both Mission and Visi.docxMost organizations, including hospitals, adopt both Mission and Visi.docx
Most organizations, including hospitals, adopt both Mission and Visi.docx
 
More like this Abstract TranslateFull Text Translate.docx
More like this Abstract TranslateFull Text Translate.docxMore like this Abstract TranslateFull Text Translate.docx
More like this Abstract TranslateFull Text Translate.docx
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 

Recently uploaded (20)

OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 

Mylib.pydef allInOne(n1, n2) return {addplus(n1, n2).docx

  • 1. Mylib.py def allInOne(n1, n2): return {"add":plus(n1, n2), "sub":subtract(n1, n2), "mult":multiply(n1, n2), "div": divide(n1, n2)} def IsInRange(lr, hr, n): return lr < n and n < hr #add 2 numbers def plus(n1,n2): add = n1 + n2 return add #subtract 2 numbers def subtract(n1, n2):
  • 2. sub = n1 - n2 return sub #multiply 2 numbers def multiply(n1, n2): mult = n1 * n2 return mult #division, n2 should not be zero def divide(n1, n2): try: div = n1/(n2 + 0.0) return div except ZeroDivisionError as e: print("The result of %s/%s = %s" % (n1, n2,"You cannot divide by Zero")) except Exception : print("Invalid input" , n1, n2)
  • 3. #expression calculator def scalc(val): try: #the epression is assumed to be comma separated ns = val.split(",") #split it into 3 parts #remove whitespaces in the components n1 = int(ns[0].strip()) n2 = int(ns[1].strip()) op = ns[2].strip() #check if operator is one of the supported if op == "*": return multiply(n1, n2) elif op == "/": return divide(n1, n2) elif op == "+": return plus(n1, n2)
  • 4. elif op == "-": return subtract(n1, n2) else: print("Unknown operator", op) #in case of invalid input catch the error except Exception : print("Invalid input:" , val) W6_first_last.py # Program name : # Student Name : # Course : ENTD220 # Instructor : # Date : 02/12/2020 # Description : Expression & Arithmetic Calculator # Copy Wrong : This is my work import Mylib def doScalc(): print("---------Expression calculator------------------") print("Enter the expression with format N1,N2,operator:") print("Supported operator * , /, -, +") print("E.g 5, 4, *)") val = input() res = Mylib.scalc(val)
  • 5. print('The result of "' + val + '" is', res) def showList(): print("1) Add two numbers") print("2) Multiply two numbers") print("3) Subtract two numbers") print("4) Divide two numbers") print("5) Scalc") print("6) All in one") print("7) Exit") def getUserInput(): low = int(input("Enter Lower range:")) high = int(input("Enter Higher range:")) n1 = int(input("Enter first number:")) n2 = int(input("Enter second number:")) return [low, high, n1 , n2] def doCalculation(op): user = getUserInput() low = user[0] high = user[1] n1 = user[2] n2 = user[3] if Mylib.IsInRange(low , high, n1) and Mylib.IsInRange(low , high, n2): if op == "+": res = Mylib.plus(n1, n2) elif op == "*": res = Mylib.multiply(n1, n2) elif op == "-": res = Mylib.subtract(n1, n2) elif op == "/": res = Mylib.divide(n1, n2) print("%d %s %d = %d" % (n1, op, n2, res))
  • 6. else: print("Error: input out of range") def doAllInOne(): user = getUserInput() low = user[0] high = user[1] n1 = user[2] n2 = user[3] if Mylib.IsInRange(low , high, n1) and Mylib.IsInRange(low , high, n2): mydict = Mylib.allInOne(n1, n2) print("%d + %d = %d" % (n1, n2, mydict["add"])) print("%d - %d = %d" % (n1, n2, mydict["sub"])) print("%d * %d = %d" % (n1, n2, mydict["mult"])) print("%d / %d = %d" % (n1, n2, mydict["div"])) def main(): while True: showList() action = int(input("What do you want to do?")) if action == 7: break; if action == 1: user = getUserInput doCalculation("+") elif action == 2: doCalculation("*") elif action == 3: doCalculation("-") elif action == 4: doCalculation("/") elif action == 5: doScalc() elif action == 6: doAllInOne()