Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

In Lab 07 we wrote a program to calculate energy bill for residential and business customers

Computer Science Mar 20, 2021

In Lab 07 we wrote a program to calculate energy bill for residential and business customers. We are going to rewrite that program with value returning functions. Residential customers pay $0.12 per kWh for the first 500 kWh. After the first 500 kWh, the rate is $0.15 per kWh. Business customers pay $0.16 per kWh for the first 800 kWh. After the first 800 kWh, the rate is $0.20 per kWh. Write a program to calculate energy charge. You must write and use the following functions.

A main function: Call the value returning function get_user_input, which returns kWh used and customer type. Pass the return values to the value returning function bill_calculator as two arguments. Display the return value of bill_calculator.

A get_user_input function: This function has no parameter. It asks the user to enter number of kWh used. Use an input validation loop to ensure that kWh used is not negative. Also ask the user to enter customer type (enter R for residential or B for business). Convert lowercase letter to uppercase. Use an input validation loop to ensure that customer is either R or B. Return kWh used and customer type.

A bill_calculator function: This function has two parameters to receive number of kWh used and customer type. Calculate and return the energy charge.

 

The following is an example.

Enter kilowatt hours used: -5

kWh cannot be negative.

Enter kilowatt hours used: -6

kWh cannot be negative.

Enter kilowatt hours used: 510

Enter R for residential customer, B for business customer: x

Invalid customer type.

Enter R for residential customer, B for business customer: y

Invalid customer type.

Enter R for residential customer, B for business customer: r

Please pay this amount: $61.50

In python 

Expert Solution

Answer:

def get_user_input():
inputs = []
while True:
kwh = int(input("Enter kilowatt hours used: \n"))
if kwh > 0:
inputs.append(kwh)
break
else:
print("kWh cannot be negative.")
while True:
letter = input("Enter R for residential customer, B for business customer: \n")
letter = letter.upper()
if letter == 'R' or letter == 'B':
inputs.append(letter)
break
else:
print("Invalid customer type.")
return inputs
def bill_calculator(kwh, type):
bill = 0
if type == 'R':
if kwh <= 500:
bill = 0.12 * kwh
else:
bill = 60 + (0.15 * (kwh - 500))
else:
if kwh <= 800:
bill = 0.16 * kwh
else:
bill = 148 + (0.20 * (kwh - 800))
return bill

inputs = get_user_input()
bill = bill_calculator(inputs[0], inputs[1])
print("Please pay this amount: $", bill)

PFA

 

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment