Re: [Tutor] Issue with Code [SOLVED]

2016-05-01 Thread Alan Gauld via Tutor
On 01/05/16 05:20, Olaoluwa Thomas wrote:
> Thank you so much, Alan. That fixed it (See Script 2[SOLVED] below).
> 
> For the purpose of record-keeping, I'm pasting the entire code of all
> scripts below as I should have done from the very beginning.
> 

thanks :-)

> P.S. How were you able to open attachments with the restrictions on this
> mailing list?

The two code attachments made it to my reader.
But that seems to be a fairly arbitrary occurence.
The screen shot didn't make it.

Some make it, others don't. I don't know the exact
set of rules that determine when an attachment
gets through!

> Script 2 [SOLVED]
> hours = float(raw_input ('How many hours do you work?\n'))
> rate = float(raw_input ('What is your hourly rate?\n'))
> if hours > 40:
> gross = ((hours - 40) * (rate * 1.5)) + (40 * rate)
> elif hours >= 0 and hours <= 40:
> gross = hours * rate
> print "Your Gross pay is "+str(round(gross, 4))

You could have left it as else rather than elif,
but otherwise that's fine.

> I'm gonna add Try and Except to make it more responsive.

I'm not sure what you mean by responsive? The only
place try/except could/should be applied is round
the float conversions. But it only makes sense if
you put them inside a loop so you can force the
user to try again if the input is invalid.

Something like:

while True:
   try:
 value = float(input(...))
 break
   except ValueError:
 print 'warning message'
-- 
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] Issue with Code [SOLVED]

2016-04-30 Thread Olaoluwa Thomas
Thank you so much, Alan. That fixed it (See Script 2[SOLVED] below).

For the purpose of record-keeping, I'm pasting the entire code of all
scripts below as I should have done from the very beginning.

P.S. How were you able to open attachments with the restrictions on this
mailing list?

Script 1
hours = raw_input ('How many hours do you work?\n')
rate = raw_input ('What is your hourly rate?\n')
gross = float(hours) * float(rate)
print "Your Gross pay is "+str(round(gross, 4))

Script 2
hours = raw_input ('How many hours do you work?\n')
rate = raw_input ('What is your hourly rate?\n')
if hours > 40:
gross = ((float(hours) - 40) * (float(rate) * 1.5)) + (40 * float(rate))
elif hours >= 0 and hours <= 40:
gross = float(hours) * float(rate)
print "Your Gross pay is "+str(round(gross, 4))

Script 2 [SOLVED]
hours = float(raw_input ('How many hours do you work?\n'))
rate = float(raw_input ('What is your hourly rate?\n'))
if hours > 40:
gross = ((hours - 40) * (rate * 1.5)) + (40 * rate)
elif hours >= 0 and hours <= 40:
gross = hours * rate
print "Your Gross pay is "+str(round(gross, 4))

I'm gonna add Try and Except to make it more responsive.

Thanks a lot!

*Warm regards,*

*Olaoluwa O. Thomas,*
*+2347068392705*

On Sun, May 1, 2016 at 2:00 AM, Alan Gauld via Tutor 
wrote:

> On 01/05/16 01:16, Alan Gauld via Tutor wrote:
>
> > I can't see anything obviously wrong in your code
>
> I was too busy focusing on the calculations that
> I didn't check the 'if' test closely enough.
> You need to convert your values from strings
> before comparing them.
>
> hours = float(raw_input ('How many hours do you work?\n'))
> rate = float(raw_input ('What is your hourly rate?\n'))
> if hours > 40:
>gross = (hours-40)*(rate*1.5) + (rate*40)
> else:
>gross = hours*rate
>
>
> Sorry,
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Issue with Code

2016-04-30 Thread Alan Gauld via Tutor
On 01/05/16 01:16, Alan Gauld via Tutor wrote:

> I can't see anything obviously wrong in your code

I was too busy focusing on the calculations that
I didn't check the 'if' test closely enough.
You need to convert your values from strings
before comparing them.

hours = float(raw_input ('How many hours do you work?\n'))
rate = float(raw_input ('What is your hourly rate?\n'))
if hours > 40:
   gross = (hours-40)*(rate*1.5) + (rate*40)
else:
   gross = hours*rate


Sorry,

-- 
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] Issue with Code

2016-04-30 Thread Matt Ruffalo
On 2016-04-30 11:30, Olaoluwa Thomas wrote:
> I would appreciate a logical explanation for why the "else" statement in
> the 2nd script isn't working properly.
>
> I'm running Python v2.7.8 on a Windows 7 Ultimate VM via Command prompt and
> my scripts are created and edited via Notepad++ v6.7.3
>

Hi-

The problem is that you're reading 'hours' and 'rate' from the user with
'raw_input', and this function returns a string containing the
characters that the user typed. You convert these to floating point
numbers before doing any processing of the gross pay, but in your
'GrossPayv2.py', you compare the string referred to by 'hours' to the
numeric value 40.

In Python 2, strings always compare as greater than integers:

"""
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "60" > 40
True
>>> "20" > 40
True
"""

This unfortunate behavior is one of the things fixed in Python 3. Unless
you have a compelling reason otherwise (like a course or textbook that
you're learning from), I would recommend using Python 3 instead of 2,
since many of these "gotcha" behaviors have been fixed in the newer (but
backward-incompatible) version of the language.

Specifically:
"""
Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "60" > 40
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: str() > int()
"""

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


Re: [Tutor] Issue with Code

2016-04-30 Thread Alan Gauld via Tutor
On 30/04/16 16:30, Olaoluwa Thomas wrote:

> I've attached the scripts in question (created via Notepad++).

attachments often get stripped by the mail system as a security risk.
Since your code is very short just post it in the mail message
as plain text.

> The problem with this script is that the "else" statement (which is
> equivalent to the 1st script) does not compute gross pay accurately as seen
> in the attached screenshot.

The screenshot seems to have been stripped so we can't see it.
Just tell us what happened and what you expected. Or better
still cut 'n paste the output into the message.

> I would appreciate a logical explanation for why the "else" statement in
> the 2nd script isn't working properly.

It almost certainly is working properly, just not as you expected
it to :-)

When dealing with floating point numbers remember that they will
always be approximations to the whole number so calculations will
similarly result in approximations. Usually if you round the
results the figures will look ok.

I can't see anything obviously wrong in your code although
it could be simplified. Please post a sample including the
inputs and outputs. Tell us what you expect to see as well
as showing us what you do see.


-- 
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