[Tutor] Tutor Digest, Vol 142, Issue 10
Ken Hammer
kfh777 at earthlink.net
Sat Dec 12 12:23:32 EST 2015
I have a more simple-minded solution. The error appears to occur in testing without revealing the problem by use of $1 base price in the test. Using $10,000 as the base price in the test reveals absurd numbers. I think the real problem lies in the inclusion of base price inappropriately, twice as in:
>dealerPrep = basePrice + 500
>destinationCharge = basePrice + 250
"basePrice" has no place in those two lines. 500 and 250 alone express the definition of the challenge.
Ken
In <mailman.13.1449939602.12987.tutor at python.org>, on 12/12/15
at 12:00 PM, tutor-request at python.org said:
>Send Tutor mailing list submissions to
> tutor at python.org
>To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>You can reach the person managing the list at
> tutor-owner at python.org
>When replying, please edit your Subject line so it is more specific than
>"Re: Contents of Tutor digest..."
>Today's Topics:
> 1. Calculation error with a simple program (Jim Gallaher)
> 2. Re: Calculation error with a simple program (Alan Gauld)
> 3. Re: Calculation error with a simple program (Laura Creighton)
>----------------------------------------------------------------------
>Message: 1
>Date: Sat, 12 Dec 2015 01:03:05 -0600
>From: Jim Gallaher <jcgallaher78 at gmail.com>
>To: tutor at python.org
>Subject: [Tutor] Calculation error with a simple program
>Message-ID: <566BC6A9.6090206 at gmail.com>
>Content-Type: text/plain; charset=utf-8; format=flowed
>Hi everyone. I'm reading through a beginners Python book and came up with
>a super simple program. I'm not getting any errors and everything runs
>through, but there's a logical calculation error. What the program does is
>take an amount and calculate a couple percentages and add a couple fees.
>For example, if I put in a value of 1, it will output 752.12 as the sub
>total and 753.12 as the grand total. It's off by 1 on sub total and 2 on
>grand total.
>Thanks in advance! Jim Gallaher
># Car Salesman Calculator
># User enters the base price of the car and the program adds tax, license,
>dealer prep, and destination charge.
>print("Car Sales Calculator")
>basePrice = int(input("Please enter in the price of the car: "))
># Misc charges to be added to the total cost of the car
>tax = basePrice * .07
>license = basePrice * .05
>dealerPrep = basePrice + 500
>destinationCharge = basePrice + 250
># Add the total misc charges together
>subTotal = float(tax + license + dealerPrep + destinationCharge)
># Add all the misic charges and include the base price
>grandTotal = float(subTotal + basePrice)
># Display the results
>print("\nThe sub total is", subTotal)
>print("\nYour grand Total is", grandTotal)
>input("\nPress the enter key to close the program.")
>------------------------------
>Message: 2
>Date: Sat, 12 Dec 2015 08:04:52 +0000
>From: Alan Gauld <alan.gauld at btinternet.com>
>To: tutor at python.org
>Subject: Re: [Tutor] Calculation error with a simple program
>Message-ID: <n4gkf4$e99$1 at ger.gmane.org>
>Content-Type: text/plain; charset=utf-8
>On 12/12/15 07:03, Jim Gallaher wrote:
>> For example, if I put in a value of 1, it will output 752.12 as the sub
>> total and 753.12 as the grand total. It's off by 1 on sub total and 2 on
>> grand total.
>Are you sure? Lets check the values...
>> basePrice = int(input("Please enter in the price of the car: "))
>=> 1
>> tax = basePrice * .07
>=> 0.07
>> license = basePrice * .05
>=> 0.05
>> dealerPrep = basePrice + 500
>=> 501
>> destinationCharge = basePrice + 250
>=> 251
>> # Add the total misc charges together
>> subTotal = float(tax + license + dealerPrep + destinationCharge)
>=> 0.07 + 0.05 + 501 + 251 => 752.12
>> # Add all the misic charges and include the base price
>> grandTotal = float(subTotal + basePrice)
>=> 752.12 + 1 => 753.12
>Looks like Python got it right to me?
--
-----------------------------------------------------------
K.F.Hammer Associates Ken Hammer
management consultations Saint Johnsbury, VT 05819
-----------------------------------------------------------
More information about the Tutor
mailing list