2013-04-18 40 views
0

我是非常新的編程和我目前正在學習從我購買的書python。 在每章的結尾處,根據您在前一章中學到的內容編寫程序時遇到挑戰。它要求爲用戶創建一個程序,以輸入餐館賬單的金額並告訴他們兩個金額15%和20%的提示,但它沒有在書中討論如何計算python的百分比。我嘗試在網上查找,沒有任何關於此事的信息。我相信這很簡單,但我不會在理解這本書之前繼續進行這本書。如何爲python創建小費程序?

+1

你知道如何獲得的金額乘以?數量是多少?使用大多數編程語言時要注意的一個有趣的事情是1/4 = 0,因爲你正在進行整數運算。這可能是你的問題。 – mattg

回答

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)) 
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) 
+0

這是有道理的,例如raw_input做什麼而不僅僅是輸入。另外爲什麼兩個%% 15.之後做了什麼?做什麼..感謝你對我的耐心只是想讓我的頭腦全部 – JamesB88

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)] 
0

您可以通過1.0,以獲得價值爲float

 
amount= 125 
tip1 = 15 
tip2 = 20 

print "tip1=" , (amount * (1.0 * tip1/100)) 
print "tip2=" , (amount * (1.0 * tip2/100)) 
相關問題