我对编程非常陌生,我目前正在从我购买的一本书中学习 python。在每一章的最后,你都面临着根据你在前一章学到的东西编写程序的挑战。它要求创建一个程序,让用户输入餐馆账单的金额,并告诉他们两个金额一个 15 % 和 20% 小费,但书中没有讨论如何在 python 中计算百分比。我曾尝试在网上查找,但没有任何相关信息。我确信这很简单,但在理解这一点之前,我不会继续阅读这本书。
问问题
8811 次
4 回答
1
bill = raw_input("Please enter restaurant total\n")
print "15 %%: %.2f" %round((float(bill)*.15),2)
print "20 %%: %.2f" %round((float(bill)*.20),2)
于 2013-04-18T15:56:31.120 回答
0
这个怎么样:
# input will prompt user for a bill amount at the command line
amount = float(input('Enter the amount of the bill: '))
tip_15 = amount * .15
tip_20 = amount * 0.2
print('A 15%% tip is: %.2f. A 15%% tip is: %.2f.' % (tip_15, tip_20))
print('Total price with 15%% tip is: %.2f' % (amount + tip_15))
print('Total price with 20%% tip is: %.2f' % (amount + tip_20))
于 2013-04-18T15:56:40.980 回答
0
def calc_tips(total, tip_percentages):
return [(x, x*total) for x in tip_percentages]
>>> print calc_tips(100, (.15, .2))
[(0.15, 15.0), (0.2, 20.0)]
于 2013-04-18T16:00:29.750 回答
0
您可以乘以 1.0 以获得浮点值
金额= 125 提示1 = 15 提示2 = 20 打印 "tip1=" , (金额 * (1.0 * tip1/100)) 打印 "tip2=" , (金额 * (1.0 * tip2/100))
于 2013-04-18T16:00:41.553 回答