Price ladder (15 minutes)

Chapter 2-3 price ladder (15 minutes)

To promote residents to conserve electricity, power companies performing province "price ladder", a mounting table of a residential user tariff is divided into two "step": Within months consumption 50 kwh (50 kWh) of price is 0.53 yuan / kWh; over 50 kwh, the excess portion of the electricity price increase 0.05 yuan / kWh. Write a program to calculate electricity.

Input formats:

Gives a user input in line with monthly consumption (unit: kWh).

Output formats:

Output line of the user tariff payable (RMB), the results of two decimals, formats such as: "cost = cope tariff value"; if power consumption is less than 0, then output "Invalid Value!".

Sample Input 1:

10

Output Sample 1:

cost = 5.30

Sample Input 2:

100

Output Sample 2:

cost = 55.50

Code

def cost(x):
  if x<0:
    print("Invalid Value!")
    exit()
  return x*0.53 if x<=50 else 0.53*50+(x-50)*0.58
  
print("cost = {:.2f}".format(cost(int(input()))))

Guess you like

Origin www.cnblogs.com/nonlinearthink/p/10991503.html