-1
我已經得到的地步,我可以計算總的伙食費給定範圍小費百分比:迭代與循環
def total_cost(price,tax,tip):
bill = price+(price*tax)+((price+(price*tax))*tip)
return bill
for tip in range(15,30,5):
cost = total_cost(20, 0.1350, tip/100.)
print(cost)
從下面的問題混合多個變量的時候我很爲難:
下面您會看到一本包含不同州稅率的詞典,用百分比表示。在所有州進行迭代並打印一份膳食的價值,其中稅前食品價格爲20美元;顯示價格的15%,20%和25%的小費百分比。」
tax = {
"Alabama": 13.50,
"Alaska": 7,
"Arizona": 10.73,
"Arkansas": 11.63,
"California": 10,
"Colorado": 10,
"Connecticut": 6.35,
"Delaware": 0,
"District of Columbia": 5.75,
"Florida": 7.50,
"Georgia": 8,
"Hawaii": 4.50,
"Idaho": 8.50,
"Illinois": 9.75,
"Indiana": 7,
"Iowa": 7,
"Kansas": 9.65,
"Kentucky": 6,
"Louisiana": 11,
"Maine": 5.50,
"Maryland": 6,
"Massachusetts": 6.25,
"Michigan": 6,
"Minnesota": 7.88,
"Mississippi": 7.25,
"Missouri": 9.35,
"Montana": 0,
"Nebraska": 7.50,
"Nevada": 8.10,
"New Hampshire": 0,
"New Jersey": 7,
"New Mexico": 8.69,
"New York": 8.88,
"North Carolina": 7.50,
"North Dakota": 8,
"Ohio": 8,
"Oklahoma": 11,
"Oregon": 0,
"Pennsylvania": 8,
"Rhode Island": 7,
"South Carolina": 9,
"South Dakota": 6,
"Tennessee": 9.75,
"Texas": 8.25,
"Utah": 8.35,
"Vermont": 7,
"Virginia": 6,
"Washington": 9.60,
"West Virginia": 7,
"Wisconsin": 5.60,
"Wyoming": 6,
}
我知道如何打印與加入的話語句,例如「稅在阿拉斯加州:7.0「,但我不知道如何取得每個稅率並插入我的功能? –
使用total_cost(價格,稅金,小費)功能。 I.e. total_cost(20.0,稅[name_of_key],15.0) – rmeertens
這就是我的想法,但我在哪裏包括它? –