首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从模块导入Python 3,5/2会给出2而不是2.5

从模块导入Python 3,5/2会给出2而不是2.5
EN

Stack Overflow用户
提问于 2015-04-25 23:12:59
回答 1查看 181关注 0票数 0

我在python 3中制作了一个计算器程序,我已经将程序分成了模块。我在一个模块中做了一个减法函数,它只取两个数字,并与正规/运算符除以。然而,它的作用似乎是/来自python 2,而5/2给我的是2而不是2.5。然而,我试着制作一个新的文件,它所做的只是打印(5/2),这给了我2.5。我试着重新安装python,但没有结果。我有最新版本(3.4.3)。我是搞砸了还是这是个虫子?

计算器:

代码语言:javascript
复制
import operation
import formula
import time
print("\n")
run = True
while run:
try:
    print("1: Addittion")
    print("2: Subtraction")
    print("3: Multiplication")
    print("4: Division")
    print("5: Advanced")
    print("6: Quit")
    print("\n")

    choice = int(input("What would you like to do? "))
    print("\n")

    if choice == 1:
        operation.add()

    elif choice == 2:
        operation.subtract()

    elif choice == 3:
        operation.multiply()

    elif choice == 4:
        operation.divide()

        try:
            answer = numberA / numberB

        except ZeroDivisionError:
            print("ERROR: DIVIDE BY ZERO")
            print("\n")

        else:
            print("%d / %d = %d" % (numberA, numberB, answer))
            print("\n")

    elif choice == 5:
        print("1: Arithmetic Sequences")
        print("2: Geometric Sequences")
        print("3: Pythagorean Theorem")
        print("4: Median")
        print("5: Go Back")
        print("\n")

        choice = int(input("What would you like to do? "))
        print("\n")

        if choice == 1:
            formula.arithmetic()

        elif choice == 2:
            formula.geometric()

        elif choice == 3:
            formula.pythagorean()

        elif choice == 4:
            operation.median()

        elif choice == 5:
            pass

        else:
            print("ERROR: PLEASE ENTER A VALID OPTION (1-5)")
            print("\n")

    elif choice == 6:
        pass
        time.sleep(3)
        run = False

    else:
        print("ERROR: PLEASE ENTER A VALID OPTION (1-6)")
        print("\n")

except ValueError:
    print("\n")
    print("ERROR: PLEASE ONLY USE NUMBERS")
    print("\n")

模块:

代码语言:javascript
复制
def divide():
numberA = int(input("What is the dividend? "))
numberB = int(input("What is the divisor? "))
print("\n")

try:
    answer = numberA / numberB

except ZeroDivisionError:
    print("ERROR: DIVIDE BY ZERO")
    print("\n")

else:
    print("%d / %d = %d" % (numberA, numberB, answer))
    print("\n")

注意:这个模块实际上还有其他方程(-,x和+),但我没有包括它们,因为我认为您不需要它们。如果你想看他们,告诉我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-25 23:28:19

使用整数说明符格式化。试一试这个方法。

代码语言:javascript
复制
print('{} / {} = {}'.format(numberA, numberB, answer))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29871776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档