十进制模块可以处理未知值吗?

时间:2019-06-21 10:56:29

标签: python decimal

我是十进制模块的新手,我不确定十进制模块是否可以读取和处理未知值。我需要更改哪些代码才能使其正常工作?

我对此进行了研究,但是找不到理想的答案

std::thread

我期望x和y相加,但是输出是的无效操作 from decimal import Decimal def Addition(x,y): sum=Decimal('x')+Decimal('y') print("The sum of {0} and {1} is {2}".format(x, y,sum)) x=float(input("Enter your first number: ")) print("Your first number is="+str(x)) y=float(input("Enter your second number: ")) print("Your second number is="+str(y)) Addition(x,y)

2 个答案:

答案 0 :(得分:1)

查看代码中的注释。

from decimal import Decimal

def Addition(x,y):
    sum=x+y   #You don't need quotes around x and y
    print("The sum of {0} and {1} is {2}".format(x, y,sum))

x=Decimal(input("Enter your first  number: "))
print("Your first number is {}".format(x)) #No need to convert to string
y=Decimal(input("Enter your second  number: "))
print("Your second number is {}".format(y)) #No need to convert to string

Addition(x,y)

输出:

Enter your first  number: 5.789
Your first number is 5.789
Enter your second  number: 5.34566
Your second number is 5.34566
The sum of 5.789 and 5.34566 is 11.13466

答案 1 :(得分:0)

IIUC你的意思是:

from decimal import Decimal
def Addition(x,y):
    sum=Decimal(x)+Decimal(y)   
    print("The sum of {0} and {1} is {2}".format(x, y,sum))
    x=float(input("Enter your first  number: "))
    print("Your first number is="+str(x))
    y=float(input("Enter your second  number: "))
    print("Your second number is="+str(y))
Addition(x,y)