plapapyjh 2022-09-30 00:18 采纳率: 43.5%
浏览 69
已结题

Python初学者遇到的基础问题

这个代码就是一个基础的计算电费的代码,如果运用字典对这个代码做一些改变,我遇到这一步卡住了,下面是它改动的要求:
Now create a version of the above electricity program that uses a dictionary to store the tariffs and the corresponding cost.
In the prompt, list all the tariffs (all the dictionary keys) and make sure a valid one is selected.
Use the appropriate cost from the dictionary to calculate the bill total.

You will need to change how you present the "Which tariff" prompt, since these values come from the dictionary.

To show the benefit of this, add three more tariffs (just make them up).

这个是原本的代码:(代码是没有问题的)

TARIFF_11 = 0.244618
TARIFF_31 = 0.136928

def electricity_bil1(price_kwh, kwh, days):
    price = price_kwh * kwh * days
    return price

def main():
    # 选择模块
    choose = input('Please select the corresponding electricity price: (input 1 or 2)') # 请选择电价
    if choose == '1':
        price_kwh = TARIFF_11
    elif choose == '2':
        price_kwh = TARIFF_31
    else:
        return -1
    # 输入信息
    kwh = int(input('Please enter the daily power consumption: '))
    days = int(input('Please enter the days of power consumption:'))
    print('The total electricity price is:',electricity_bil1(price_kwh, kwh, days),'cents')
main()

  • 写回答

2条回答 默认 最新

  • 进自欢 2022-09-30 02:05
    关注

    是这样吧

    
    TARIFF={'1':0.244618,'2':0.136928}
     
    def electricity_bil1(price_kwh, kwh, days):
        price = price_kwh * kwh * days
        return price
     
    def main():
        # 选择模块
        choose = input('Please select the corresponding electricity price: (input 1 or 2)') # 请选择电价
        if choose in TARIFF:
            price_kwh = TARIFF [choose]
        else:
            return -1
        # 输入信息
        kwh = int(input('Please enter the daily power consumption: '))
        days = int(input('Please enter the days of power consumption:'))
        print('The total electricity price is:',electricity_bil1(price_kwh, kwh, days),'cents')
    main()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月8日
  • 已采纳回答 9月30日
  • 赞助了问题酬金1元 9月30日
  • 创建了问题 9月30日