Py学习  »  Python

如何删除以python结尾的浮点数?[副本]

Muratek • 4 年前 • 245 次点击  

这个问题已经有了答案:

这里是一个初学Python编程的学习者。

我正在用python制作一个非常简单的两位数计算器。虽然它运行得很好,但我无法解决以“.0”结尾的浮点数问题。我想删除这个结尾。我试了几次,但都不管用。我需要在代码中添加什么条件?你能看一下下面吗?

    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()

提前谢谢!

我完全按照@water winter的建议做了,但每当我执行程序并输入数字和运算符时,就会出现以下错误: if result.is_integer(): AttributeError: 'int' object has no attribute 'is_integer'

更新:看起来只有“/”操作符工作得很好。其他3个操作符给出了上述相同的错误。:

新的更新:最后我设法使计算器编程完美!

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()

谢谢你们的帮助和建议。他们帮了很多忙!:)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38625
 
245 次点击  
文章 [ 1 ]  |  最新文章 4 年前
thithien
Reply   •   1 楼
thithien    5 年前

首先,有一个内置函数用于 float 检查 integer 还有 int() 去掉小数点。

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

其次,如果你只是想 print , the return 关键字是不必要的。

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()