Python, calculator class definition

150
March 28, 2022, at 12:30 PM

I'm struggling with a practice problem below where I'm confused as to what the definitions would look like.

Create a class definition called 'calc' which is a calculator that supports these operators: +, -, *, / It should have the following methods:

def firstNumber(self, value):
def secondNumber(self, value):
def operator(self,op):
def answer(self):
def history(self):

Test Example:

c = calc()
c.firstNumber(4)
c.secondNumber(5)
c.operator('+')
print(c.answer())
#Should print 9 here.
c.firstNumber(5)
c.secondNumber(5)
c.operator('*')
print(c.answer())
#should print 25 here
print(c.history())
#should print:
#1.) 4 + 5 = 9
#2.) 5 * 5 = 25
Answer 1
class Calculator(): #Create a class
    def calculate(): #Make function for calculate
        firstNumber = int(input("Enter your first number: ")) #Input first number and convert into interger
        secondNumber = int(input("Enter your second number: ")) #Input second number and convert into interger
        operator = input("Enter your operator: ") #Input operator
        if operator == "+": #Check if operator is +
            return firstNumber + secondNumber #Return result
        elif operator == "-": #Check if operator is -
            return firstNumber - secondNumber #Return result
        elif operator == "*": #Check if operator is *
            return firstNumber * secondNumber #Return result
        elif operator == "/": #Check if operator is /
            return firstNumber / secondNumber #Return result
Calc = Calculator() #Call the class
Result = Calc.calculate() #Calculate
print(Result) #Print the result into the console

So this is a basic calculator which will wait for our input for the firstNumber and then convert it to integer and also do the same thing with the secondNumber

Then it let us input the operator which is +, -, *, / and check if the input is one of an operator and then return the result

This is a very basic calculator, lambda or something I forget will make a calculator that same as the calculator you have seen, like when you input 1 + 6 - 3 it will return 4

================================================================

Btw the history function I think it's hard for you cuz you need to do a database which very complex for beginner

Rent Charter Buses Company
READ ALSO
python pydantic How to choose class based on another value

python pydantic How to choose class based on another value

some of example of schemaI have different constraints based on type

147
How to fit UMAP object with an embedding_ property of shape (200,2)

How to fit UMAP object with an embedding_ property of shape (200,2)

I'm trying to take the first 200 rows of my dataframe and pass it through a UMAP fit method but I'm not able to get it to shape the way I want at (200,2)Should I add an embedding parameter?

104
Django - passing one model to another while creating new object

Django - passing one model to another while creating new object

My site has multiple tests with multiple questions eachI'd like to make question creation form which will have preset test object depended on url

107
Import could not be resolve after installing the dependency

Import could not be resolve after installing the dependency

I'm trying to install de messagebird dependency into my python project

171