这个代码就是一个基础的计算电费的代码,如果运用字典对这个代码做一些改变,我遇到这一步卡住了,下面是它改动的要求:
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()