我正在编写计算器,需要帮助,允许用户输入浮动

时间:2015-04-03 19:32:34

标签: python python-3.x calculator

好的,这是我的代码,它在python 3.4.3中,我不知道如何允许用户输入为浮点数。任何帮助将不胜感激。 它是一个计算器并且工作正常,但它不允许用户输入为浮点数(具有小数位),并且使用十进制数的输入进行大量计算,因此它有点需要它。如果您花时间阅读,谢谢!

import time

def cls(): print ("\n"*100)
def add():
cls()

print("you have selected addition")

a = input("Enter your first number: ")
while a.isdigit() == False:
    print("Enter a numerical interger")
    a = input("Enter your first number: ")
if a.isdigit() == True:
    a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
    print("Enter a numberical interger")
    b = input ("enter your second number: ")
if b.isdigit() == True:
    b = int(b)
print ("\n")
print ("ANSWER:",a,"+",b,"=",a+b)
print ("\n")

def sub():
    cls()
    print("you have selected subtraction")
    a = input("Enter your first number: ")
    while a.isdigit() == False:
        print("Enter a numerical interger")
        a = input("Enter your first number: ")
    if a.isdigit() == True:
        a = int(a)
    b = input("Enter your second number: ")
    while b.isdigit() == False:
    print("enter a numerical interger")
    b = input("Enter your second number: ")
    if b.isdigit() == True:
    b = int(b)
    print("\n")
    print ("ANSWER:",a,"-",b,"=",a-b)
    print("\n")
def multi():
    cls()
    print ("you have selected multiplication")
    a = input("Enter your first number: ")
    while a.isdigit() == False:
        print("Enter a numerical interger")
        a = input("Enter your first number: ")
    if a.isdigit() == True:
        a = int(a)
    b = input("Enter your second number: ")
    while b.isdigit() == False:
        print("enter a numerical interger")
        b = input("Enter your second number: ")
    if b.isdigit() == True:
        b = int(b)
    print("\n")
    print("ANSWER:",a,"*",b,"=",a*b)
    print("\n")
def divide():
    cls()
    print ("you have selected division")
    a = input("Enter your first number: ")
    while a.isdigit() == False:
        print("Enter a numerical interger")
        a = input("Enter your first number: ")
    if a.isdigit() == True:
        a = int(a)
    b = input("Enter your second number: ")
    while b.isdigit() == False:
        print("enter a numerical interger")
        b = input("Enter your second number: ")
    if b.isdigit() == True:
        b = int(b)
    c = (a/b)
    if a%b ==0 :
        print("\n")
        print ("ANSWER:",a,"/",b,"=",int(c))
        print("\n")
    else :
        print("\n")
        print ("ANSWER:",a,"/",b,"=",float(c))
        print("\n")

def indice():
    cls()
    print ("you have selected indice multiplication")
    a = input("Enter your first number: ")
    while a.isdigit() == False:
        print("Enter a numerical interger")
        a = input("Enter your first number: ")
    while (int(a)) >=1000000000000:
        print("value too high, enter a lower value")
        time.sleep(1)
        a = input("Enter your first number: ")
    if a.isdigit() == True:
        a = int(a)
    b = input("Enter your second number: ")
    while b.isdigit() == False:
        print("enter a numerical interger")
        b = input("Enter your second number: ")
    while (int(b)) >=1000:
        print("value too high, enter a lower value")
        time.sleep(1)
        b = input("Enter your second number: ")  
    if b.isdigit() == True:
        b = int(b)
    print("\n")
    print("ANSWER:",a,"To the power of",b,"=",a**b)
    print("\n")

def Tconv():
    cls()
    print("You have selected unit conversion")
    print("\n")
    print("Enter 1 for conversion from celcius")
    print("Enter 2 for conversion from kelvin")
    print("\n")
    a = input("Enter your choice: ")
    if a == "1":
        cls()
        Tcelc()
    elif a == "2":
        cls()
        Tkelv()
    else:
        print("Not a valid entry, try again")
        time.sleep(1)
        cls()
        Tconv()

def Tcelc():
    print("You have selected conversion from celcius")
    print("\n")
    a = input("Enter your celcius value: ")
    if a.isdigit() == False:
        print("Not a valid entry")
        time.sleep(1)
        cls()
        Tcelc()
    elif a.isdigit() == True:
        print("\n")
        print("AWNSER = ",(int(a))+273,"Kelvin")
        print("\n")

def Tkelv():
    print("You have selected conversion from kelvin")
    print("\n")
    a = input("Enter your kelvin value: ")
    if a.isdigit() == False:
        print("Not a valid entry")
        time.sleep(1)
        Tkelv()
    elif a.isdigit() == True:
        print("ANSWER = ",(int(a))-273,"Celcius")
        print("\n")

def OpEx():
        cls()
        print("what operation would you like to preform?")
        print("\n")
        print("Enter 1 for addition")
        print("Enter 2 for subtraction")
        print("Enter 3 for multliplication")
        print("Enter 4 for division")
        print("Enter 5 for indice multiplication")
        print("Enter 6 for unit conversion")
        print("\n")
        print("Or type 'close' to exit the program")
        print("\n")
        task = input("enter your choice: ")
        print("\n")
        if task == "1":
            add()
            menu()
        elif task == "2":
            sub()
            menu()
        elif task == "3":
            multi()
            menu()
        elif task == "4":
            divide()
            menu()
        elif task == "5":
            indice()
            menu()
        elif task == "6":
            Tconv()
            menu()
        elif task == "close":
            exit()
        else:
            print ("not a valid entry")
            time.sleep(2)
            OpEx()
def menu():
    Q1 = input("Type 'yes' to preform a calculation type 'no' to exit: ")
    if Q1 == "yes":
        OpEx()
    if Q1 == "no":
        print("sorry I could not be of futher service")
        time.sleep(1)
        exit()
    else:
        print("\n")
        print("Not a valid entry, try again")
        print("\n")
        time.sleep(1)
        cls()
        menu()
cls()
menu()

2 个答案:

答案 0 :(得分:1)

您正在将用户输入转换为整数,这些整数不能很好地处理浮点数。请尝试转换为float,例如:

a = float(a)

答案 1 :(得分:-1)

我会谨慎地将输入作为一个浮点数,因为python和许多其他语言浮点数并不像它们看起来那样表示。我建议将输入拉为字符串,然后再将其转换。