Home > Back-end >  I tried to make a calculator but everytime i try subtraction it still makes addition
I tried to make a calculator but everytime i try subtraction it still makes addition

Time:11-08

I tried to make a basic calculator by myself. I am complately new and that is why ı don't really know where did ı made the mistake. I can make an addition but still when ı try to make subtraction ıt makes addition again. It ıs my first project and ı need help. I am waiting for your responds :)

mathematical_operation=input("Choose your mathematical operation ")
print(mathematical_operation)


def addition(str):
    "addition"
    print(str)
    return

if mathematical_operation:= 'addition':

    first=input("first: ")
    print("first")
    second=input("second: ")
    print("second")
    sum=float(first)   float(second)
    print("sum"  str(sum))

def subtraction(str):
        "subtraction"
        print(str)
        return

if mathematical_operation:='subtraction':
    first=input("first: ")
    print("first")
    second=input("second: ")
    print("second")
    dif=float(first) -float(second)
    print("Sum"  str(dif))

CodePudding user response:

That's because you used := (walrus) instead of == operator.

When you want to compare values, use == so replace all your:

if mathematical_operation:='subtraction':

by:

if mathematical_operation == 'subtraction':

(same goes for "addition")

CodePudding user response:

you need to change input types to integers for calculation.

first=int(input("first: ")) # converting str to int
print("first")
second=int(input("second: ")) # converting str to int
print("second")
sum=float(first)   float(second)
print("sum"  str(sum))
  • Related