Python 3, 5/2 从模块导入时给出 2 而不是 2.5
Python 3, 5/2 when importing from module gives 2 and not 2.5
我在 python 3 中制作了一个计算器程序,我已将该程序拆分为多个模块。我在一个模块中做了一个减法函数,它只接受两个数字并用普通的 / 运算符进行除法。然而,它似乎表现得像 / from python 2 和 5/2 给了我 2 而不是 2.5。但是,我尝试制作一个新文件,它所做的只是 print(5/2),这给了我 2.5。我尝试重新安装 python 无济于事。我有最新版本 (3.4.3)。我是搞砸了还是这是一个错误?
计算器:
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")
模块:
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 和 +),但我没有包括它们,因为我认为您不需要它们。想看就告诉我
您使用整数说明符进行格式化。请试试这个方法。
print('{} / {} = {}'.format(numberA, numberB, answer))
我在 python 3 中制作了一个计算器程序,我已将该程序拆分为多个模块。我在一个模块中做了一个减法函数,它只接受两个数字并用普通的 / 运算符进行除法。然而,它似乎表现得像 / from python 2 和 5/2 给了我 2 而不是 2.5。但是,我尝试制作一个新文件,它所做的只是 print(5/2),这给了我 2.5。我尝试重新安装 python 无济于事。我有最新版本 (3.4.3)。我是搞砸了还是这是一个错误?
计算器:
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")
模块:
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 和 +),但我没有包括它们,因为我认为您不需要它们。想看就告诉我
您使用整数说明符进行格式化。请试试这个方法。
print('{} / {} = {}'.format(numberA, numberB, answer))