Python请求十进制数字
有人能告诉我怎么让代码询问一个数值吗?比如用户输入100.00,然后把这个数字和它的两位小数保存下来,以便后面可以用来做其他操作,比如乘法等等。
谢谢。
2 个回答
0
在Python的命令行中试试下面的代码。
>>> int(100.00)
100
>>>
3
这是一个开始的例子(适用于Python 3;如果你用的是Python 2,请用raw_input
代替input
):
while True:
snum = input("Please enter a decimal number:")
try:
num = float(snum)
break
except ValueError:
print("This is not a valid decimal number!")
print("This number, rounded to two places, is: {:0.2f}".format(num))