1

A beginner python programming learner here.

I'm making a very simple two number calculator in python. Although it runs perfectly, I can't fix the issue with floating numbers ending with ".0". I want to remove this ending. I tried a few things but they didn't work. What condition do I need to add inside the code? Could you please take a look at it below:

    def calculator():
        num1 = float(input('First number: '))
        operator = input('+, -, / or * ? ')
        num2 = float(input('Second Number: '))

        if operator == '+':
            return print(num1, '+', num2, '=', num1 + num2)
        elif operator == '-':
            return print(num1, '-', num2, '=', num1 - num2)
        elif operator == '/':
            return print(num1, '/', num2, '=', num1 / num2)
        elif operator == '*':
            return print(num1, '*', num2, '=', num1 * num2)

calculator()

Thank you in advance!

I exactly did what @water-winter suggested but whenever I execute the program and enter numbers and operator I get this error: if result.is_integer(): AttributeError: 'int' object has no attribute 'is_integer'

Update: Looks like that only "/" operator works perfectly. The other 3 operators give the same error above. :/

New Update: Finally I managed to make the calculator programming flawlessly!

def calculator():
    num1 = float(input("First number: "))
    operator = input("Choose: +, -, /, or *")
    num2 = float(input("Second number: "))
    num1 = int(num1) if num1.is_integer() else num1
    num2 = int(num2) if num2.is_integer() else num2
    add = (num1 + num2)
    subtract = (num1 - num2)
    divide = (num1 / num2)
    multiply = (num1 * num2)

    if operator == "+" and add % 1 == 0:
        print(num1, "+", num2, "is equal to:", int(add))
    elif operator == "+" and not add % 1 == 0:
        print(num1, "+", num2, "is equal to:", add)
    elif operator == "-" and subtract % 1 == 0:
        print(num1, "-", num2, "is equal to:", int(subtract))
    elif operator == "-" and not subtract % 1 == 0:
        print(num1, "-", num2, "is equal to:", subtract)
    elif operator == "/" and divide % 1 == 0:
        print(num1, "/", num2, "is equal to:", int(divide))
    elif operator == "/" and not divide % 1 == 0:
        print(num1, "/", num2, "is equal to:", divide)
    elif operator == "*" and multiply % 1 == 0:
        print(num1, "*", num2, "is equal to:", int(multiply))
    elif operator == "*" and not multiply % 1 == 0:
        print(num1, "*", num2, "is equal to:", multiply)

calculator()

Thank you guys, for the help and suggestions. They helped a lot! :)

Muratek
  • 11
  • 3
  • a float is going to always have a decimal component. An int will not. Read about format specifiers for how you can print a string representation of a float with an arbitrary number of decimals. – tehhowch Aug 25 '18 at 00:59
  • Show us what you have tried! – Klaus D. Aug 25 '18 at 01:00

1 Answers1

0

First, there's a built-in function for float to check for integer and there's int() to trip off decimal places.

>> float(0.5).is_integer()
>> False
>> float(3.0).is_integer()
>> True
>> int(float(5.5))
>> 5
>> int(float(3))
>> 3

Second, if you simply want to print, the return keyword is unnecessary.

def calculator():
    num1 = float(input('First number: '))
    operator = input('+, -, / or * ? ')
    num2 = float(input('Second Number: '))

    num1 = int(num1) if num1.is_integer() else num1
    # This line is equivalent to:
    # if num1.is_integer():
    #     num1 = int(num1)
    # else:
    #     num1 = num1

    num2 = int(num2) if num2.is_integer() else num2

    if operator == '+':
        result = num1 + num2
        result = int(result) if result.is_integer() else result
        print(num1, '+', num2, '=', result)
    elif operator == '-':
        ...
calculator()
thithien
  • 235
  • 1
  • 12
  • 2
    Note: Many `float` operations that, with real numbers, would result in an integer value will not do so with `float`s. `1.1 * 3 - 0.3` is, to any fifth grader, supposed to result in `3`. But in `float` math, it comes out to `3.0000000000000004` (on my machine anyway), and `is_integer()` on the result returns `False`. If you need decimal precision, I'd recommend using `decimal.Decimal`, not `float`. – ShadowRanger Aug 25 '18 at 02:18