python calculator with two float numbers as parameters [closed]

2024/11/21 14:40:11

Write a menu driven program using functions to make a calculator having the following operations: Add, Subtract, Multiply and Divide.

  1. Define four functions, with two float numbers as parameters and calculate the answer. The function need not return any value as we can directly print the answer from there.
  2. Create a menu using the print command with the respective options and take an input choice from the user.
  3. Using if/elif statements for cases and call the appropriate functions.
  4. Give the user an option to restart the calculator. To implement this, put the entire code in a while loop with the condition that the input from user should be, 'y' or 'Y'.

Can someone please help me with the code?

Answer

Hope this code may help you

def add(a,b):print(a+b)
def subract(a,b):print(a-b)
def multipy(a,b):print(a*b)
def divide(a,b):print(a/b)
ch="y"
while ch=="y" or ch=="Y":x = float(input("first number : "))y = float(input("second number: "))print(".....MENU.......\n 1.Add\n 2.Subtract\n 3.Multiply\n 4.Divide\n")op=int(input("Enter your choice : "))if op==1:add(x,y)elif op==2:subract(x,y)elif op==3:multipy(x,y)elif op==4:divide(x,y)else: print("invalid Choice")ch=input("Do you want to continue?(Y/y) : ")

you may get output as:

    first number : 10
second number: 20
.....MENU.......1.Add2.Subtract3.Multiply4.DivideEnter your choice : 1
30.0
Do you want to continue?(Y/y) : y
first number : 20.7
second number: 13.2
.....MENU.......1.Add2.Subtract3.Multiply4.DivideEnter your choice : 2
7.5
Do you want to continue?(Y/y) : y
first number : 3.6
second number: 7.9
.....MENU.......1.Add2.Subtract3.Multiply4.DivideEnter your choice : 3
28.44
Do you want to continue?(Y/y) : y
first number : 45
second number: 7
.....MENU.......1.Add2.Subtract3.Multiply4.DivideEnter your choice : 4
6.428571428571429
Do you want to continue?(Y/y) : nProcess finished with exit code 0

this is a simple basic problem ... First you have to try your own code then if you get any error while solving. You have to ask Don't just directly post your question..

https://en.xdnf.cn/q/120759.html

Related Q&A

python - Is it possible to combine 2 functions together to create 1 function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

How to create a grading system in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python lists comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Import everyday date for one year

Is there a simple way to import into my Python file a list with all the dates for a given year? What I need is to store everyday that a year has (01.01.2018----31.12.2018) into a list, so I can then i…

Object detection realtime using tensorflow

Im trying to detect objects in realtime using tensorflow. . I ran jupyter notebook in object_detection directory. then I opened the notebook file. It is firing the following errorIm getting the followi…

How do I get Python to read and extract words from a text file? [duplicate]

This question already has answers here:How to copy files(21 answers)Closed 7 years ago.So I need to make a code that opens a txt file, and then takes the content of that file and puts it into another t…

How to draw a checkered flag to the Python screen?

QUESTION: Implement the following pseudocode to draw a checkered flag to the screen.1. Ask the user for the size of the checkered flag (n). 2. Draw an n x n grid to the screen. 3. For i = 0,2,4,...,…

Open txt file in python

i need to open a txt file . In txt file i have Andrei:Popescu:Bucuresti Maria:Popescu:Targu-Mures ....How do I read a text file into three variable and for each line do something ? Sorry for my englis…

Python 3.3.3 time.sleep() error [duplicate]

This question already has answers here:Python: "global name time is not defined"(8 answers)Closed 10 years ago.Im getting the following error:The error:"Traceback (most recent call last)…

what is the decimal.getcontext().copy() mean [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…