# Objective
To write a program that calculates kilowatt - hour electrical appliance usage.
# PROJECT DESCRIPTION
Write a program that calculates the annual cost of operating various home appliances. For each appliance, the program will request that the user enter the cost per kilowatt - hour and the number of kilowatt - hours the appliance uses in a year.
A sample run of the program is shown below illustrating the input gathered and a resulting output total displayed.
[ please enter the requested data ]
appliance name: central air
cost per KW - hr of the appliance (in cents): 0.17
annual usage (in KW - hr): 1400
appliance name: dishwasher
cost per KW - hr of the appliance (in cents): 0.17
annual usage (in KW - hr): 25
appliance name: electric range
cost per KW - hr of the appliance (in cents): 0.16
annual usage (in KW - hr): 52
appliance name: refrigerator
cost per KW - hr of the appliance (in cents): 0.15
annual usage (in KW - hr): 175
appliance name: toaster
cost per KW - hr of the appliance (in cents): 0.15
annual usage (in KW - hr): 4
appliance name: washing machine
cost per KW - hr of the appliance (in cents): 0.16
annual usage (in KW - hr): 9
The total cost of the annual usage is $ 278.86.
# Information about This Project
[ Sequential Program Control ]
The three types of program control include sequential, selection and repetition. Sequential program control arises when the execution of the programming statements occurs in a step by step fashion without any decisions or repetition.
[ Kilowatt - Hours ]
When you purchase electricity you are charged by the kilowatt - hour ( kWh ) . For example, when you use 1,000 watts for 1 hour, that is equivalent to one kilowatt - hour.
A kilowatt - hour is defined as a measure of electrical energy equivalent to a power consumption of 1,000 watts for 1 hour.
# Steps to Complete This Project
# STEP 1 Open the MS Visual Studio or the Python IDLE IDE
Open MS Visual Studio for Python, Python IDLE IDE ( Integrated Development Environment ) , or similar Python development environment on your computer.
[ MS Visual Studio ]
Open Microsoft Visual Studio, click [ Create a new project ] and at the next window, highlight the [ Python Application ] template and click on Next.
In the Configure your new Project dialog box, add in a desired Project name example, Lab1, and check where is says ‘Place solution and project in the same directory’. Press Create to finish and proceed to the newly created Python source file, where you will write your program code.
[ VSCode ]
For VSCode, open up a workspace to work with by choosing File > Add Folder to Workspace ... , where the Workspace is a directory for which you have read and write privileges. Choose a directory to work with then click [ Add ] . You should now see your workspace folder to add files to. If you don’t see it in your editor, choose View > Explorer. To start a new File, right click on your workspace folder and choose New File. Name your file with a desired file name with a .py extension in the text field that now appears.
[ Python IDLE ]
You will notice when you initially open Python IDLE, the default is an interpretive shell allowing only for single commands to be given. You really need to enter in a whole program then execute it to work any of the labs for the course.
To start entering code into IDLE, go to File > New File from your menu. This will allow you to enter your source code in an editor style format like Notepad.
At any point, you can save your file. It will prompt you to do so when you go to run your program. You may save your file anytime to a designation of your choice. The extension will always be .py.
# STEP 2 Write the Program Code for this Application
In your editor, you may begin typing your source code. Here are some thoughts to assist you in writing the program code.
For all your labs, include comments, where applicable in your code that explains what your assigning such as variables or what a loop will perform or which method will execute, etc.
To enter in a Python comment anywhere in code include a hash tag ( # ) .
[ Example ]
# local variable declarations | |
totalCost = 0.0 | |
# declare variable as a float type to accumulate total charges |
Have your program perform the following logic:
Write the program code that will allow the user to enter the necessary input items listed in the project description area above ( check the sample run provided ) and then use these items to compute the required output value, that is, the annual usage charge. The annual usage charge is based on cost * kWh usage as mentioned above per a given appliance. Accumulate a total of all annual usage charges and display your result at the end of the program.
Hints to set up your code:
- Declare and assign the required variables for the program. For example: appliance, cost, usage, annual cost, total cost.
- For each appliance listed on page 1 in the sample run section, prompt the user for the necessary input items ( the appliance name, the cost per kilowatt - hour and the annual usage in kWh ) .
- As each annual cost is computed, update the total cost variable, which serves as an accumulator variable.
- Display the total cost usage at the end of your program.
Some starter code is given below for your convenience.
# local variable declarations | |
totalCost = 0.0 | |
# declare variable as a float type to accumulate total charges | |
appName = "" | |
# declare a variable for the appliance name | |
costPerKW = 0.0 | |
# declare a variable for the cost per KW - hr | |
annualUsage = 0.0 | |
# declare a variable for the annual usage | |
print ("[ please enter the requested data ]") | |
print ("appliance name:") | |
appName = input() | |
print ("cost per KW - hr of the appliance (in cents):") | |
costPerKW = float(input()) | |
print ("annual usage (in KW - hr):") | |
annualUsage = float(input()) | |
#print("Total thus far $%.2f" % (costPerKW * annualUsage)) |
# STEP 3 Compile and Run Your Program
Run your program.
[ MS Visual Studio ]
To run your program, use [ Ctrl ] + [ F5 ] .
[ VSCode ]
It is simple to run your program with Python. Just right click on your file and choose the Run Python File in Terminal option or just click on the Run Python File button in the top - right side of the editor.
[ Python IDLE ]
To run your program, go to your menu and choose Run > Run Module or press [ F5 ] on your keyboard to run your program.
# STEP 4 Test Your Program
Test the operation of your program using for your input, the data shown on page 1 in the sample area. Make sure your total cost ties out to $ 278.86 !
# STEP 5 Submit Your Program Code and Your Run Time Output
When completed, include a a snapshot of the program’s completed output followed by a copy and pasting of your program’s source code into Word as well and submit your file into Blackboard (BB). Name your word doc filename appropriately, ex. sstudent_Lab1.
*Include your name, date, lab number and course number descriptions at the top part of your source file as well as your name, date/time, and lab number on your output as well. This should be performed for all your subsequent labs.
*Grads include the following stats for output as well, namely the average, variance and standard deviations from all costs where…
average = sum KW/hr / number of cost items
variance = (variance + (average – costPerItem) ** 2) / item count
stdDev = variance ** .5
Display your output above up to the 4th decimal place.
-See lab 1 grad calcs Excel file in BB for sample totals-
# STEP 6 Questions and Answers Concerning this Computer Laboratory Project
What is meant by Sequential Program Control?
Without using selection control or repetitive control, how would you modify the program to account for a coupon, for a new energy - saving appliance, that the program user can implement to lower the total cost?
What is the purpose of adding comment statements?
What is the function of the interpreter?
Why is it important to test your program?
# Solutions
# Code
# local variable declarations | |
# declare variable as a float type to accumulate total charges | |
totalCost = 0.0 | |
# create list | |
applist = [] | |
# declare a variable for the appliance name | |
appName = "" | |
# declare a variable for the cost per KW - hr | |
costPerKW = 0.0 | |
# declare a variable for the annual usage | |
annualUsage = 0.0 | |
# declare a variable for number of cost items | |
costItems = 6 | |
# declare a variable for totalKwhr | |
totalKwhr = 0 | |
# declare a variable for variance | |
variancePerKW = 0.0000 | |
print ("[ please enter the requested data ]") | |
for x in range(costItems,0,-1): | |
appName = input("Appliance name %.d: " % (costItems + 1 - x)) | |
costPerKW = float(input("The cost per KW - hr of the appliance (in cents):")) | |
annualUsage = float(input("The annual usage (in KW - hr):")) | |
totalKwhr += costPerKW | |
totalCost += costPerKW * annualUsage | |
applist.append([appName, costPerKW, annualUsage]) | |
print("\t\n") | |
print("%-10s: $%.2f" % ("Total Annual Cost", totalCost)) | |
averageKwhr = totalKwhr / costItems | |
print("%-10s: $%.4f" % ("Average", averageKwhr)) | |
for i in applist: | |
variancePerKW += (averageKwhr - i[1]) ** 2 | |
variance = variancePerKW / costItems | |
print("%-10s: $%.4f" % ("Variance", variance)) | |
print("%-10s: $%.4f" % ("stdDev", variance ** .5)) |
# Q&A
What is meant by Sequential Program Control?
A control system in which the individual steps are processed in a predetermined order, progression from one sequence step to the next being dependent on defined conditions being satisfied.
Without using selection control or repetitive control, how would you modify the program to account for a coupon, for a new energy – saving appliance, that the program user can implement to lower the total cost?
I will add a parameter input to enter the discount for the coupon, and then multiply the discount when calculating the total cost.
couponDiscount = float(input("The discount:"))
totalCost += costPerKW * annualUsage * couponDiscount
What is the purpose of adding comment statements?
The comment statements are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.
What is the function of the interpreter?
The interpreter's job is to take these code objects and follow the instructions. An interpreter is a kind of program that executes other programs. When you write Python programs , it converts source code written by the developer into intermediate language which is again translated into the native language / machine language that is executed.
Why is it important to test your program?
Testing is important because it finds defects/errors before delivery to customers, thus ensuring the quality of the software. It makes the software more reliable and easy to use. Fully tested software ensures reliable and high-performance software operation.