这样我就可以写" sales ";和"总数"有两个小数点,但是我似乎不能把价格打印成两个小数点,因为我在试图正确格式化它时总是遇到错误。
price = float(input("Price: $ "))
tax = .05
salestax = (price*tax)
total = ((price*tax)+ price)
print("Sales Tax: $", format(salestax,'.2f'))
print("Total: $", format(total,'.2f'))
有什么建议吗?
Price: $99.00
Sales Tax: $4.95
Total: $103.95
如果你有3.6+,我认为这是一个很多更酷的实现f-strings:
import os
from getpass import getpass
print('Price: $ ', end='', flush=True)
price = float(getpass(''))
# Clears the line with prompt and input
clear = 'cls' if os.name == 'nt' else 'clear'
os.system(clear)
tax = .05
salestax = price * tax # remove redudant parentheses here
total = price * tax + price # remove again, redundant paranthesis (mult before add)
print(f'Price: $ {price:.2f}')
print(f'Sales Tax: $ {salestax:.2f}')
print(f'Total: $ {total:.2f}')
注意:注意getpass
的使用。它不像你想象的那样在交互式终端上工作。您需要一个常规的终端,如windows提示符来查看预期的输出。
测试上面的输入:
$ Price: $ 123.456789
Price: $ 123.46
Sales Tax: $ 6.17
Total: $ 129.63