簡體   English   中英

如何為python創建小費程序?

[英]How do i create a tipping program for python?

我對編程非常陌生,目前正在從購買的書中學習python。 在每一章的最后,您都需要根據上一章的知識編寫程序,要求創建一個程序來讓用戶輸入餐廳賬單金額,並告訴他們兩個金額15 %和20%的小費,但書中並未討論如何計算python中的百分比。 我嘗試過在線查找,對此一無所獲。 我相信這很簡單,但是在理解這一點之前,我不會對本書進行進一步的介紹。

bill = raw_input("Please enter restaurant total\n")
print "15 %%: %.2f" %round((float(bill)*.15),2)
print "20 %%: %.2f" %round((float(bill)*.20),2) 

這個怎么樣:

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

您可以乘以1.0以獲得浮點數

amount= 125
tip1 = 15
tip2 = 20

print "tip1=" , (amount * (1.0 * tip1/100))
print "tip2=" , (amount * (1.0 * tip2/100))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM