Python class, function novice, question... "T"

Asked 2 years ago, Updated 2 years ago, 19 views

It's been a while since you've studied class and you're making it by yourself. I want to make an addition, subtraction, division, multiplication calculator. I'm a beginner. I'm using my knowledge to create coding An error occurs in the process of calculating after receiving all the input values. I think there's something wrong with the class or function Can you tell me what's wrong? First of all, it's stuck while implementing addition

class cal:
    def plus(self,a,b):
        return self,a+b

    def minus(self,a,b):
        return self,a-b

    def multi(self,a,b):
        return self, a*b

    def half(self, a,b):
        return self, a/b

while True :

    start = input ("+ : plus, - : subtract, * : multiply, / : divide : ")

    if start == '+':
        num1 = int (input ("Enter a number to add: "))))
        num2 = int (input ("Enter a number to add")
        total = (num1,num2)
        total = cal
        total.plus()

python

2022-09-22 14:57

1 Answers

Hello!

First

   def plus(self,a,b):
        return self,a+b

There is no need for self after the return statement.

And the last three lines

total = (num1,num2) # I'll erase this
Total = cal #Add () here to make the class an object!
Put num1 and num2 in total.plus() #plus()

Then


class cal:
    def plus(self, a,b):
        return a+b


while True :

    start = input ("+ : plus, - : subtract, * : multiply, / : divide : ")

    if start == '+':
        num1 = int (input ("Enter a number to add: "))))
        num2 = int (input ("Enter a number to add")
        total = cal()
        total.plus(num1,num2)

It's going to be like this, right? Have a nice day.


2022-09-22 14:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.