3.8. Figuring out an Invoice¶
We can use variables to solve problems like those we might solve in a spreadsheet. Imagine that you had a spreadsheet with an invoice for an office supply company.
We don’t really have to create new variables
unitPrice2. We only use those to compute the total for the line, and then we could reuse those variable names.
It is best to use variable names that make sense like
quantity instead of names that don’t make any sense like
Fred. The name should help you remember what the variable is representing.
Let’s say that apples are $0.40 apiece, and pears are $0.65 apiece. Modify the program below to calculate the total cost (it should print 3.55).
You are welcome to try out the following answers by copying and pasting them into the program above before answering this question:
Write the code to calculate and print how many paperclips you can buy if each paperclip is $0.05 and you have $4.00 in your pocket. It should print 80.
Create variables to hold each value. Calculate
budget / costPerClip. Be sure to print the result.
Discuss topics in this section with classmates.