如何在Python中打印十进制数值

4
print("enter start() to start the program")

def start():
    print("This script converts GBP into any currency based on the exchange rate...")
    print(" ") #enters a line
    exchangeRate = int(input("Enter the exchange rate (Eg: 0.80)"))
    print("how much would you like to convert???")                       
    gpb = int(input())
    print(gpb*exchangeRate)

如果我将汇率设置为0.81,然后输入1英镑时,它总是返回0。

2个回答

7

使用float()代替int()来处理input()的调用。例如:

    gpb = float(input())

否则,如果用户输入了0.81,则在转换过程中int()将截断为0
通过使用float(),您将保留所提供的小数值,并且计算应产生您期望的结果。

0

你将类型指定为int.....这些是整数(0,1,2,3,4,5,6,7,8....),当你将1乘以0.81时,得到的结果是0.81.....整数的关键数字是小数点前面的数字,在这种情况下是零。所以就像之前的回答简要提到的那样,只需更改变量的类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接