for循环

时间:2017-05-09 11:59:08

标签: python

我已经达到了可以计算总膳食成本的点,给出了一系列倾翻百分比:

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,
}

1 个答案:

答案 0 :(得分:2)

我认为如果你自己解决这个问题会更好,你会从中学到更多。

我可以给你的提示是这个代码迭代你给出的字典中的每个键,并在该状态下打印税。

for name_of_key in tax:
  print("Tax in state %s: %f" % (name_of_key, tax[name_of_key]))