In: Computer Science
Python problem
Residential and business customers are paying different rates for water usage. Residential customers pay $0.005 per gallon for the first 6000 gallons. If the usage is more than 6000 gallons, the rate will be $0.007 per gallon after the first 6000 gallons. Business customers pay $0.006 per gallon for the first 8000 gallons. If the usage is more than 8000 gallons, the rate will be $0.008 per gallon after the first 8000 gallons. For example, a residential customer who has used 9000 gallons will pay $30 for the first 6000 gallons ($0.005 * 6000), plus $21 for the other 3000 gallons ($0.007 * 3000). The total bill will be $51. A business customer who has used 9000 gallons will pay $48 for the first 8000 gallons ($0.006 * 8000), plus $8 for the other 1000 gallons ($0.008 * 1000). The total bill will be $56. Write a program to do the following. Ask the user which type the customer it is and how many gallons of water have been used. Calculate and display the bill.
The following are some examples:
Enter R for residential customer or B for business customer:
B
How many gallons of water were used? 9000
Please pay this amount: 56
Enter R for residential customer or B for business customer:
R
How many gallons of water were used? 9000
Please pay this amount: 51
Please show your output in the answer, thank you!
#---------- calculator.py --------- python 3.7
#function that returns the resident pay for the given amount of
gallons
def getResidentPay(gallons):
if(gallons > 6000):
pay = (6000 * 0.005) +
((gallons-6000) * 0.007)
else:
pay = gallons * 0.005
return pay
#function that returns the business pay for the given amount of
gallons
def getBusinessPay(gallons):
if(gallons > 8000):
pay = (8000 * 0.006) +
((gallons-8000) * 0.008)
else:
pay = gallons * 0.006
return pay
#function to check valid choice is entered or not
def isValidChoice(choice):
if(choice == "R" or choice == "B" or
choice=="Q"):
return True
else:
return False
#main function to test
def main():
choice = ""
#till user wants to quit.
while(choice != "Q"):
#get choice
choice = input("\nEnter R for
residential customer or B for business customer or Q for Quit:
").strip().upper()
#check is valid input
if(isValidChoice(choice)):
#if
resident
if(choice ==
"R"):
try:
#get gallons
#if non numeric value is
given exception will be caught and displayes
gallons =
int(float(input("How many gallons of water were
used?").strip()))
#if number of gallons is
<=0 display error message
if(gallons<=0):
print("Invalid Number of Gallons Entered. Gallons must be >
0")
else:
#else call
the resident pay
print("Please pay this amount: %.2f"%getResidentPay(gallons))
except Exception:
print("Gallons must be a
Number")
elif(choice ==
"B"):
try:
#if float values are entered
convert them into int
gallons =
int(float(input("How many gallons of water were
used?").strip()))
if(gallons<=0):
print("Invalid Number of Gallons Entered. Gallons must be >
0")
else:
#else call
the Business pay
print("Please pay this amount: %.2f"%getBusinessPay(gallons))
except Exception:
print("Gallons must be a
Number")
else:
print("Exiting...")
else:
print("Invalid
Option Entered.")
if __name__ == "__main__":
main()