[Tutor] Calculation error with a simple program

2015-12-12 Thread Jim Gallaher
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.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculation error with a simple program

2015-12-12 Thread Alan Gauld
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?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculation error with a simple program

2015-12-12 Thread Laura Creighton
I haven't verified this, but you are probably up against some
problem caused by using floating point to store money.

The problem is that, when people see something like the floating
point number, 3.15 they think, aha, this is a decimal number just
like I learned in school when I was 10 years old.  This is 3 plus
one-tenth, plus 5 one hundreths. exactly.  I know everything about this
sort of thing, I learned it when I was 10.

In particular, you probably learned this properly of numbers in
general.

(a + b) + c  = a + (b + c)  (associative properly of addition)

And while you probably had not thought about this property, you
definitely exepected it to work with your 3.15 when you want to add
it to 2.17

And, herein lies the rub.
floating point numbers, despite looking exactly the same as what you
learned in school (A terrible design decision, I think that if we
wrote them 3_15 or 3#15 we would have many fewer problems now, but
it got made before I was born, so  ) ARE NOT the things you
learned in school.

Not all decimal numbers are exactly representable in floating point.
As a result the law of association fails.  The correct result can 
indeed depend on the order in which you do your operations.

You can read more about this here:
https://docs.python.org/3.5/faq/design.html#why-are-floating-point-calculations-so-inaccurate

and in the tutorial:

https://docs.python.org/3.5/tutorial/floatingpoint.html#tut-fp-issues

This is also good.
http://floating-point-gui.de/

And if you want a more serious, mathematical approach, this ACM reprint is
_really good_, but not to everybody's taste.
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Most of the time, this does not matter, because most of the people
using floating point are scientists.  Scientists, contrary to popular
belief, are not all that concerned with exact accuracy.  Their results
always come with an error estimate.  'the distance is such and such 
+/- 10%' or +/- 0.005%  or what have you.  So, as long as you do
your calculations in such a way that the floating point error is
covered in the % of uncertainty that you have anyway, your answer
is perfectly correct enough for science.

And floating calculations are much, much faster than fixed point
decimal calculations, which in the old days meant the difference
between getting a result for your calculations in a week or in
more than a month.  No wonder the scientists were happy to trade
accuracy which they couldn't use for speed, which they had immediate
need for.

But once you get to calculations with money, you are suddenly out of
the realm of science.  (Even if some economists might want to tell
you different. :) )  Money is exact.   Nobody is willing to accept
10 pounds +/- 10% for a pizza, and allow people to pay anything between
9 pounds and 11 pounds as an ok price.

In python we have a fix for this, called the Decimal object.
Decimals behave the way you want them to -- i.e. the principle of
associaton holds.

https://docs.python.org/3.5/library/decimal.html

So, if you are just noodling around, playing with things, using floats
to store money is ok.  It teaches bad habits, but that battle was 
fought and lost a long time ago.  However, if you want to do something
serious, like keep track of your own financial affairs with python,
***Don't Use Float For Money***.  Please save yourself grief and
use the Decimal type instead.

Laura
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculation error with a simple program

2015-12-12 Thread Steven D'Aprano
On Sat, Dec 12, 2015 at 01:03:05AM -0600, Jim Gallaher wrote:
> 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.

Check your arithmetic -- with a base price of $1, the sub total is 
$752.12 and the grand total is $753.12, exactly as Python calculates.


But I wonder whether you have made a logic mistake in your code. You 
say:

dealerPrep = basePrice + 500
destinationCharge = basePrice + 250

This means that the car will cost more than TRIPLE the base price. 
Instead of $1, enter a base price of $40,000 and you will see what I 
mean: now the dealer prep is $40500 and the destination charge is 
$40250, which added to the base price gives $120750 (plus tax and 
licence). Surely that's not right, the deal charges more than the cost 
of the car for preparation? I think what you want is:

dealerPrep = 500
destinationCharge = 250



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculation error with a simple program

2015-12-12 Thread Jim Gallaher
Hi Alan,

I'm 100 percent sure I'm wrong. :-) I verified it when I fixed the mistake. The 
problem was it was adding in the basePrice and the fixed rates/percentages each 
time. So I figured it out when Ian said something about that.

Thanks for everyone's help! :-)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculation error with a simple program

2015-12-12 Thread Todd Purple via Tutor


> On Dec 12, 2015, at 2:03 AM, Jim Gallaher  wrote:
> 
> 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
> 

I think your main problem is right here. Why do dealerPrep and 
destinationCharge include the basePrice in their equation? This will add the 
basePrice in multiple times. I would simply set it like this:

dealerPrep = 500
destinationCharge = 250

If these charges ever change, you can put in some sort of equation. However, it 
looks like these are meant to be a flat fee. 

> # Add the total misc charges together
> subTotal = float(tax + license + dealerPrep + destinationCharge)
> 
> # Add all the misic charges and include the base pricem
> 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.")
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 142, Issue 10

2015-12-12 Thread Ken Hammer

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 , on 12/12/15 
   at 12:00 PM, tutor-requ...@python.org said:



>Send Tutor mailing list submissions to
>   tutor@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-requ...@python.org

>You can reach the person managing the list at
>   tutor-ow...@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 
>To: tutor@python.org
>Subject: [Tutor] Calculation error with a simple program
>Message-ID: <566bc6a9.6090...@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 +
>From: Alan Gauld 
>To: tutor@python.org
>Subject: Re: [Tutor] Calculation error with a simple program
>Message-ID: 
>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
---

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Beautiful Soup

2015-12-12 Thread Crusier
Dear All,

I am trying to scrap the following website, however, I have
encountered some problems. As you can see, I am not really familiar
with regex and I hope you can give me some pointers to how to solve
this problem.

I hope I can download all the transaction data into the database.
However, I need to retrieve it first. The data which I hope to
retrieve it is as follows:

"
15:59:59 A 500 6.790 3,395
15:59:53 B 500 6.780 3,390

Thank you

Below is my quote:

from bs4 import BeautifulSoup
import requests
import re

url = 
'https://bochk.etnet.com.hk/content/bochkweb/eng/quote_transaction_daily_history.php?code=6881&time=F&timeFrom=09&timeTo=16&turnover=S&sessionId=44c99b61679e019666f0570db51ad932&volMin=0&turnoverMin=0'

def turnover_detail(url):
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html,"html.parser")
data = soup.find_all("script")
for json in data:
print(json)

turnover_detail(url)

Best Regards,
Henry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor