在python中编程相对较新,感谢您在我上一个问题上提供的所有快速帮助,我在另一个python项目上。无论如何,我为python项目编写了一个新程序,为餐饮场所制作账单。这是我的下面的代码,一切运行正常,我得到了项目所需的预期结果,我遇到的两个问题是,1。我需要沙漠的成本不打印---> 3.0但是---> $ 3.00,基本上,我如何打印美元符号,以及e.x 3.0 - > 3.00或45.0 - > 45.00 ..和价格之前的美元符号。对不起,如果有人要求这样的话......
import math
# constants
Cost_Per_Desert = 3.00
Tax_Rate = .075
Gratuity_Tips = .15
Adult_Meal_Cost = 12.75
Child_Meal_Cost = .60*12.75
Room_Fee = 450.00
Less_Deposit = 250.00
def main():
# Input Section
Name = input("\n\n Customer:\t\t\t ")
Number_Of_Adults = int(input(" Number of Adults:\t\t "))
Number_Of_Children = int(input(" Number of Children:\t\t "))
Number_Of_Deserts = int(input(" Number of Deserts:\t\t "))
print("\n\nCost Of Meal Per Adult:\t\t" , Adult_Meal_Cost)
print("Cost of Meal Per Child:\t\t" , round(Child_Meal_Cost,2))
print("Cost Per Desert:\t\t" , round(Cost_Per_Desert,2))
# Processing/Calculations
Total_Adult_Meal_Cost = Adult_Meal_Cost* Number_Of_Adults
Total_Child_Meal_Cost = Child_Meal_Cost* Number_Of_Children
Total_Desert_Cost = Cost_Per_Desert* Number_Of_Deserts
Total_Food_Cost = Total_Adult_Meal_Cost + Total_Child_Meal_Cost + Total_Desert_Cost
Total_Taxes = Total_Food_Cost * Tax_Rate
Tips = Total_Food_Cost * Gratuity_Tips
Total_Bill = Total_Food_Cost + Total_Taxes + Tips + Room_Fee
# Output Section
print("\n\n Total Cost for Adult Meals: \t", Total_Adult_Meal_Cost)
print(" Total Cost for Childs Meals: \t", Total_Child_Meal_Cost)
print(" Total Cost for Desert: \t", Total_Desert_Cost)
print(" Total Food Cost: \t\t", Total_Food_Cost)
print("\n\n Plus 7.5% Taxes: \t\t", round(Total_Taxes,2))
print(" Plus 15.0% Tips: \t\t", round(Tips,2))
print(" Plus Room Fee: \t\t", Room_Fee)
print("\n\n Total Bill: \t\t\t", round(Total_Bill,2))
print(" Less Deposit: \t\t\t", Less_Deposit)
print("\n\nBalance Due: \t\t\t", round(Total_Bill - Less_Deposit,2))
print("\n\n\n\n\t\t Thank You For Using Passaic County Catering Services. ")
main()
input("\n\n\n\n\nPress Enter to Continue")
答案 0 :(得分:1)
让我们说沙漠的成本是3.00美元
cost = 3
print("${0:.2f}".format(cost))
输出:
$3.00
答案 1 :(得分:0)
print('pi is %.2f' % 3.14159)
输出将是pi is 3.14
您可能想阅读:https://docs.python.org/2.7/library/string.html#formatspec