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 "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
"""

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

Reply via email to