我的结果如何更准确?

时间:2013-12-14 17:27:30

标签: python calculator

我已经制作了一个小小的计算器脚本,当我去除分数时,让我们说9 / 6.它弹出1.但我希望它更精确并弹出1.5(确切的结果)

#!/usr/bin/python

import os

def cls():
    os.system(['clear','cls'][os.name == 'nt'])

cls()
print "Do you want to [1]Add[2]Subtract[3]Multiply[4]Divide?"
k = input("Choose a number next to the choice you want: ")

if (k == 1):
    print "Enter the two numbers you want to add"
    a = input("Number 1: ")
    b = input("Number 2: ")
    cls()
    c = a + b
    print a,"+",b,"=",c
if (k == 2):
    print "Enter the two numbers you want to subtract"
    a = input("Number 1: ")
    b = input("Number 2: ")
    cls()
    c = a - b
    print a,"-",b,"=",c
if (k == 3):
    print "Enter the two numbers you want to multiply"
    a = input("Number 1: ")
    b = input("Number 2: ")
    cls()
    c = a * b
    print a,"*",b,"=",c
if (k == 4):
    print "Enter the two numbers you want to divide"
    a = input("Number 1: ")
    b = input("Number 2: ")
    cls()
    c = a / b
    print a,"/",b,"=",c

1 个答案:

答案 0 :(得分:0)

您基本上希望符合Python 3标准。只需在顶部使用from __future__ import division,它在Python 3中的行为相同。

其他方式包括在数字后使用点:9.0/6而不是9/6float(9)/6

在Python 3中,正常除法/将始终导致浮点数,除非您使用两个斜杠://